zoukankan      html  css  js  c++  java
  • java的注解

    1.常用的注解

    @overrive//重写的方法时建议都添加该注解,防止我们不是重写方法

    @deprecated  //废弃的方法

    @suppresswarning  //警告信息,属性值all表示所有

    2.元注解

    @target描述注解使用的范围

    说明

    @target(value=ElementType.值)

    属性值:

    Type: 用在类,接口上

    Field:用在成员变量上

    Method: 用在方法上

    Paramete:用在参数上

    Constructor :用在构造方法上

    Local_VARIABLE:用在局部变量上

    @retention 描述注解作用域

    @retention(RetentionPolicy.值)

    SOURCE:注解只存在于Java源代码中,编译生成的字节码文件中就不存在了。

    CLASS:注解存在于Java源代码、编译以后的字节码文件中,运行的时候内存中没有,默认值。

    RUNTIME:注解存在于Java源代码中、编译以后的字节码文件中、运行时内存中,程序可以通过反射获取该注解。//利用反射必须使用

    3.创建注解//注释的获取见反射

    public class AnnotationDemo {
        public static void main(String[] args) {
            AnnotationDemoTest1 annotationDemo1 = new AnnotationDemoTest1();
            Class<? extends AnnotationDemoTest1> class1 = annotationDemo1.getClass();
            if (class1.isAnnotationPresent(ReflectAnnotationdemo1.class)) {
                ReflectAnnotationdemo1 annotation = class1.getAnnotation(ReflectAnnotationdemo1.class);
                System.out.println(annotation.value());
            }
        }
    }
    
    // RetentionPolicy 枚举 RetentionPolicy.RUNTIME 运行时起作用 这样才能反射
    @Retention(RetentionPolicy.RUNTIME)
    // ElementType 枚举 ElementType.TYPE 修饰于类,接口,枚举,注解
    @Target(ElementType.TYPE)
    @interface ReflectAnnotationdemo1 {
        // String test()
        // value可以省略 value= 
        String value();
    }
    
    @ReflectAnnotationdemo1("111")
    class AnnotationDemoTest1 {
    
    }
    public class AnnotationDemo2 {
        public static void main(String[] args) {
            AnnotationDemoTest2 annotationDemo1 = new AnnotationDemoTest2();
            Class<? extends AnnotationDemoTest2> class1 = annotationDemo1.getClass();
            if (class1.isAnnotationPresent(ReflectAnnotationdemo2.class)) {
                ReflectAnnotationdemo2 annotation = class1.getAnnotation(ReflectAnnotationdemo2.class);
                System.out.println(annotation.test());
                System.out.println(annotation.test1()[0]);
            }
        }
    }
    
    @Retention(RetentionPolicy.RUNTIME)
    @interface ReflectAnnotationdemo2 {
        //设置默认的值
        String  test() default "111";
        int [] test1() default {1,2};
        
    }
    
    @ReflectAnnotationdemo2
    class AnnotationDemoTest2 {
    
    }
    public class AnnotationDemo3 {
        public static void main(String[] args) {
            AnnotationDemoTest3 annotationDemo1 = new AnnotationDemoTest3();
            Class<? extends AnnotationDemoTest3> class1 = annotationDemo1.getClass();
            if (class1.isAnnotationPresent(ReflectAnnotationdemo3.class)) {
                ReflectAnnotationdemo3 annotation = class1.getAnnotation(ReflectAnnotationdemo3.class);
                System.out.println(annotation.test());
                System.out.println(annotation.test1()[0]);
            }
        }
    }
    
    @Retention(RetentionPolicy.RUNTIME)
    @interface ReflectAnnotationdemo3 {
        //设置默认的值
        String  test() default "111";
        int [] test1() default {1,2};
        
    }
    
    @ReflectAnnotationdemo3(test="222",test1={4,5})
    class AnnotationDemoTest3 {
    
    }

     4.Documented注解表明这个注释是由 javadoc记录,在默认情况下也有类似的记录工具。 如果一个类型声明被注释了文档化,它的注释成为公共API的一部分。

  • 相关阅读:
    直接用ISO文件在linux上安装新系统
    vsftpd config备忘
    失恋那回事~~~
    Java之深入JVM(0) 序
    No.6 ThreadLocal类及应用技巧
    Java之多线程(1) Race Condition引起的性能问题
    NO.10 Callable与Future的应用
    NO.5 线程范围内共享变量的概念与作用(二)
    NO.5 线程范围内共享变量的概念与作用(一)
    NO.7多个线程之间共享数据的方式探讨
  • 原文地址:https://www.cnblogs.com/gg128/p/9311301.html
Copyright © 2011-2022 走看看