几个内置注解及元注解

注解和反射

1.1、 几个内置常用注解

//抑制警告,可以修饰在类和方法上
@SuppressWarnings("all")
public class Test01 {
    //@Override 重写的注解
    @Override
    public String toString() {
        return super.toString();
    }
    //@Deprecated 不推荐程序员使用,但是可以使用,或者存在更好的方法
    @Deprecated
    public static void test(){
        System.out.println("Deprecated");
    }

    public static void main(String[] args) {
        test();
    }
}
  1. @Override 方法重写的注释
  2. @Deprecated 不推荐程序员使用,但是可以使用,或者存在更好的方法
  3. @SuppressWarnings("all") 抑制警告,可以修饰在类和方法等上,需要改变响应的参数

1.2、 元注解

public class Test02 {

    @MyAnnotation
    public void test(){

    }
}

//定义一个注解
//Target表示这个注解可以用在哪些地方
@Target(value = {ElementType.METHOD,ElementType.TYPE})

//Retention 表示我们注解在什么地方有效
//runtime > class > sources
@Retention(value = RetentionPolicy.SOURCE)

//Documented  表示我们的注解是否生成在JAVAdoc中
@Documented

//Inherited 子类可以继承父类的注解
@Inherited
@interface MyAnnotation{

}

元注解有四个,如下:

  1. @Targe (value = {ElementType.METHOD,ElementType.TYPE}) 表示这个注解可以用在哪些地方,通过参数来决定
  2. @Retention(value = RetentionPolicy.SOURCE) 表示我们注解在什么地方有效,通过参数来决定
  3. @Documented 表示我们的注解是否生成在JAVAdoc中
  4. @Inherited 子类可以继承父类的注解
刚刚参加工作,很有很多不懂不会的,发现错误,欢迎指正,谢谢!
原文地址:https://www.cnblogs.com/xd-study/p/13197387.html