注解是从JDK5.0引入的新技术,注解不是程序本身,可以对程序作出解释,并且可以被其他程序(比如编译器)读取和解析。 最常见的一个注解就是@Override注解,override注解用于重写某个函数
public class Test {@Overridepublic String toString(){return super.toString();}
}
在注解中还可以添加一些参数值,以方便注解的使用,而且注解的使用位置十分自由,可以附加在package, class, method, field上,相当于给他们添加辅助信息,可以通过反射机制对这些注解的参数进行访问
常用内置注解
元注解
元注解的作用就是负责注释其他注解,Java定义了4个标准的元注解类型,用于提供对其他annotation类型的说明
我们通过创建一个注解来学习元注解的使用方式:
@Target(value = {ElementType.TYPE, ElementType.METHOD})
@Retention(value= RetentionPolicy.RUNTIME)
@Documented
@interface myAnnotation{// 注解的参数:参数类型+参数名()String name() default ""; // 设置默认值,这样可以不用传入参数也可以使用int id() default -1; // 如果默认值为-1则表示不存在String[] friend() default {};
}
可以看到,当前的注解可以使用在TYPE和METHOD两种类型上,这是通过@Target规定的,想要查看支持什么类型可以前往源码中查看
@Retenion的RUNTIME表示该注解在运行时有效。该注解的值有三个,分别是Source,表示注解仅在源码中起作用;Class,注解在编译的.class文件中也起作用;Runtime,表示在运行时起作用,其作用范围大小为source 存在n个参数的时候,在使用注解的时候也需要传入n个参数,此时若为参数设定默认值,则可以不传入该参数值也不会报错,注解会使用默认参数值 使用@interface自定义注解的时候,会自动集成java.lang.annotation.Annotation接口 反射Reflection是Java被视为准动态语言的关键,反射机制允许程序要执行器借助Reflection API获取任何类的内部信息,并且能够直接操作任意对象的内部属性和方法。在反射中,加载完类后,在堆内存的方法区中就产生了一个Class对象,这个Class对象包含有类的结构信息,可以通过此查看类的结构,请看示例: 反射机制可以实现的功能: 优点: 缺点: 分析上述实例,我们使用Class.forName(String name)方法去反射获得实例的,而查看forName的源码,我们会发现他是调用Obejct类中有一个getClass()方法去获得实例的,getClass会返回当前对象的Class类,这个类是Java反射的源头,也就是可以通过对象反射求出类的名称。 对于每个类,JRE都为其保留了一个不变的Class对象,一个Class中包含了特定的某个结构的有关信息,可以是class/interface/enum/annotation/primitive type/void。Class类的相关属性如下: Class常用函数 所有可以获取Class对象的类型如下:public enum RetentionPolicy {/*** Annotations are to be discarded by the compiler.*/SOURCE,/*** Annotations are to be recorded in the class file by the compiler* but need not be retained by the VM at run time. This is the default* behavior.*/CLASS,/*** Annotations are to be recorded in the class file by the compiler and* retained by the VM at run time, so they may be read reflectively.** @see java.lang.reflect.AnnotatedElement*/RUNTIME
}
自定义注解
@Target(value = {ElementType.TYPE, ElementType.METHOD})
@Retention(value= RetentionPolicy.RUNTIME)
@interface myAnnotation2{String value() default "";
}public class Test {//不需要写成@myAnnotation2(value="111")@myAnnotation2("111")public String test(){return "Annotation";}
}
注解的实现机制:反射
Class c = Class.forName("java.lang.String");
可以实现动态创建对象和编译,有很大的灵活性
对性能有影响,因为反射是一种解释操作反射实例
package reflection;public class Test02 {public static void main(String[] args) throws ClassNotFoundException {// 通过反射获取对象Class> c1 = Class.forName("reflection.User");Class> c2 = Class.forName("reflection.User");Class> c3 = Class.forName("reflection.User");// 输出结果为class reflection.User,可知通过forName方法获取到了Class对象System.out.println(c1);// 输出结果为true,证明一个类在内存中只有一个Class对象,所有User类的实例都会映射到该对象上// 一个类倍加在后,整个结构都会被封装在class中System.out.println(c3.hashCode()==c2.hashCode());}
}// 实体类
class User{private String name;private int id;private int age;public User() {}public User(String name, int id, int age) {this.name = name;this.id = id;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getId() {return id;}public void setId(int id) {this.id = id;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "User{" +"name='" + name + '\'' +", id=" + id +", age=" + age +'}';}
}
Class clazz = Person.class
Class clazz = person.getClass();
获取Class的方法
package reflection;public class Test03 {public static void main(String[] args) throws ClassNotFoundException {Person person = new Student();System.out.println(person.name);// 通过已有对象获得Class c1 = person.getClass();// 通过forName获得Class c2 = Class.forName("reflection.Student");// 通过类名.class获得Class
注意:只要元素类型和维度一样,那么就是同一个Class,比如有多个Person对象p1,p2,p3,那么p1的getClass得到的Class对象和p2,p3使用该方法获取到的是一样的