元注解
元注解
元注解
就是描述注解的注解
// Anno.java
@Target({ElementType.FIELD, ElementType.TYPE, ElementType.METHOD}) // 指定了注解使用的位置(成员变量,类,方法)
@Retention(RetentionPolicy.RUNTIME) //指定该注解的存活时间
//@Inherited //指定该注解可被继承
public @interface Anno {
}
// Person.java
@Anno
public class Person {
}
// Student.java
public class Student extends Person{
public void show(){
System.out.println("student....show.....");
}
}
//StudentDemo.java
public class StudentDemo {
public static void main(String[] args) throws ClassNotFoundException {
// 获取到Student类的字节码文件对象
Class clazz = Class.forName("com.charley.myanno4.Student");
// 获取注解
boolean result = clazz.isAnnotationPresent(Anno.class);
System.out.println(result);
}
}