1. 首页
  2. 编程面试题
  3. Java
  4. Java基础

什么是元注解?



元注解就是描述注解的注解

|元注解名|说明|
|-|-|
|@Target|指定了注解能在哪里使用|
|@Retention|可以理解为保留时间(生命周期)|
|@Inherited|表示修饰的自定义注解可以被子类继承|
|@Documented|表示该自定义注解,会出现在API文档里面|


@Target({ElementType.FIELD,ElementType.TYPE,ElementType.METHOD})  //指定注解使用的位置(成员变量,类,方法)
@Retention(RetentionPolicy.RUNTIME) //指定该注解的存活时间
//@Inherited //指定该注解可以被继承
public @interface Anno {
}

@Anno
public class Person {
}

public class Student extends Person {
    public void show(){
        System.out.println("student.......show..........");
    }
}

public class StudentDemo {
    public static void main(String[] args) throws ClassNotFoundException {
        //获取到Student类的字节码文件对象
        Class clazz = Class.forName("com.itheima.myanno4.Student");

        //获取注解。
        boolean result = clazz.isAnnotationPresent(Anno.class);
        System.out.println(result);
    }
}

发布者:admin,如若转载,请注明出处:https://ai1024.vip/26852.html

QR code
//