美文网首页
注解 Annotation

注解 Annotation

作者: Minstrel_a7ca | 来源:发表于2018-07-18 19:59 被阅读0次

    What

    @XXX
    注解是代码的注释,声明这个类,这个方法,这个字段是什么,需要干什么,是干什么的。
    本身不会做什么事情。
    解释过程可以指定编译器处理,或者运行期通过反射来处理。
    例如定义一个运行期来处理的注解

    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyAnnotation{
      String value();
    }
    

    基本语法
    函数返回值只能是 String|基本类型|枚举|Class|它们的数组
    函数不能有参数
    函数可以有默认值
    可以定义value()且其他函数都有默认值没有其他函数String value(),外部可以这样写@xxx("xxxString")
    定义

    public | abstract public @interface xxx{
      //  String|基本类型|枚举|Class|它们的数组 函数名() |+ default defaulValue;
      String[] strs();
    //可以有默认值
      String name() default "ddd";  
    }
    

    上述例子

    //String数组可以只定义一个String,因为name定义了默认值,所以可以不赋值。
    @xxx{strs="aa"}
    class test{
    }
    

    相关文章

      网友评论

          本文标题:注解 Annotation

          本文链接:https://www.haomeiwen.com/subject/bieapftx.html