java之--------注解

注解:

  1. AnnotationJDK5.0开始引入的;

  2. Annotation的作用:

    可以被其他程序(比如:编译器)读取;

    可以对程序做出解释;

  3. 内置注解的例子:

    @Override:重写方法的注解;

    @Deprecated:过时的方法的注解,不推荐使用,但是可以使用;

    @SuppressWarinnings(“all”):可以放在类上面,方法上面,警告的注解,加上它,可以取消警告;

  4. 元注解的例子

    @Target:作用域;

    @Documents:运行时级别

    @Retention:生成doc文档时使用

    @Inherited:自雷可以继承父类的注解

  5. 自定义注解:

    @MyAnnotation(value = "hyf",age = 1,id = 2,schools = {"a"})
    @SuppressWarnings("all")
    public class Test1 {
        @MyAnnotation
        @MyAnnotation3("123")
        public static void main(String[] args) {
    
        }
    }
    //作用域
    @Target(value = {ElementType.METHOD, ElementType.TYPE, ElementType.FIELD})
    @Retention(RetentionPolicy.RUNTIME)
    @interface MyAnnotation {
        String value() default "";
    
        int age() default 0;
    
        int id() default -1;
         //-1代表不存在
        String[] schools ()default {""};
    
       
    }
    @interface MyAnnotation3{
        String value();
    }
    
    

猜你喜欢

转载自blog.csdn.net/qq_40791843/article/details/91130262