java注解

注解的理解

  1. 注解也被成为元数据,用于修饰解释包,类,方法,属性,构造器,局部变量等数据信息
  2. 和注释一样,注解不影响程序逻辑,单注解可以被编译或运行,相当于嵌入在代码中的补充信息。
  3. 在JavaSE中,注解的使用目的比较简单,例如标记果实的功能,忽略井道等。在JavaEE中注解占据了更重要的角色,例如用来配置应用程序的任何切面,代替javaEE旧版中所一六的繁荣代码和XML配置等
基本的Annotation介绍

使用Annotation时要在其面前增加@符号,并把该Annotation当成一个修饰符使用。用于修饰它支持的程序元素

三个基本的Annotation:
  1. @Override:限定某个方法,时重写父类方法,该注解只能用于方法

    public class Override_ {
        public static void main(String[] args) {
    
        }
    }
    
    class Father{//父类
        public void fly(){
            System.out.println("Father fly");
    
        }
    }
    
    class Son extends Father{//子类
        //1.@Override注解放在fly方法上,表示子类的fly方法时重写了父类的fly
        //2.这里如果没有写@Override,还是重写了父类fly
        //3.如果你写了@Override注解,编译器就会去检查该方法是否真的重写了父类的方法,
        // 如果的确重写了,则编译通过,如果没有构成重写,则编译错误
        //4.@Override的定义
        /*
        @Target(ElementType.METHOD)
        @Retention(RetentionPolicy.SOURCE)
        public @interface Override {
        }
         */
        //补充说明:@interface的说明:@interface不是interface ,是注解类是jdk1.5之后加入的
        @Override
        public void fly() {
            System.out.println("Son fly");
        }
    }
    
  2. @Deprecated:用于表示某个程序元素(类,方法,字段,包,参数等)已过时

    public class Deprecated_ {
        public static void main(String[] args) {
    
    
        }
    }
    //1.@Deprecated修饰某个元素,表示该元素已经过时
    //2.即不再推荐使用,但是仍然可以和使用
    //3.查看@Deprecated的源码
    /*
    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})
    public @interface Deprecated {
    }
     */
    
    //4.可以修饰方法,类,字段,包,参数等
    //5.@Documented可以做到新版本和旧版本的兼容和过渡
    @Deprecated
    class A{
        public int n1 = 1;
    }
    
  3. @SuppressWarnings:抑制编译器警告

    public class SuppressWarnings {
    
    
        //1.当我们不希望看到警告的时候,可以使用@SuppressWarnings注解来抑制警告信息
        //2.在{""}中可以写入希望抑制(不显示)的警告
        //3.关于SuppressWarnings作用范围与你放置的位置有关
        /*
    
        1	all	             to suppress all warnings (抑制所有警告)
        2	boxing	         to suppress warnings relative to boxing/unboxing operations(抑制装箱、拆箱操作时候的警告)
        3	cast	         to suppress warnings relative to cast operations (抑制映射相关的警告)
        4	dep-ann	         to suppress warnings relative to deprecated annotation(抑制启用注释的警告)
        5	deprecation	     to suppress warnings relative to deprecation(抑制过期方法警告)
        6	fallthrough	    to suppress warnings relative to missing breaks in switch statements(抑制确在switch中缺失breaks的警告)
        7	finally	        to suppress warnings relative to finally block that don’t return (抑制finally模块没有返回的警告)
        8	hiding	        to suppress warnings relative to locals that hide variable
        9	incomplete-switch	to suppress warnings relative to missing entries in a switch statement (enum case)(忽略没有完整的switch语句)
        10	nls	            to suppress warnings relative to non-nls string literals(忽略非nls格式的字符)
        11	null	        to suppress warnings relative to null analysis(忽略对null的操作)
        12	rawtypes	    to suppress warnings relative to un-specific types when using generics on class params(使用generics时忽略没有指定相应的类型)
        13	restriction	    to suppress warnings relative to usage of discouraged or forbidden references
        14	serial	        to suppress warnings relative to missing serialVersionUID field for a serializable class(忽略在serializable类中没有声明serialVersionUID变量
        15	static-access	to suppress warnings relative to incorrect static access(抑制不正确的静态访问方式警告)
        16	synthetic-access	    to suppress warnings relative to unoptimized access from inner classes(抑制子类没有按最优方法访问内部类的警告)
        17	unchecked	        to suppress warnings relative to unchecked operations(抑制没有进行类型检查操作的警告)
        18	unqualified-field-access	to suppress warnings relative to field access unqualified (抑制没有权限访问的域的警告)
        19	unused	            to suppress warnings relative to unused code (抑制没被使用过的代码的警告)
         */
        //@SuppressWarnings源码
        //放置的位置就是TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE
        //该注解类有数组
        /*
            @Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
            @Retention(RetentionPolicy.SOURCE)
            public @interface SuppressWarnings {
                    String[] value();
            }
         */
        @java.lang.SuppressWarnings({""})
        public static void main(String[] args) {
            List list = new ArrayList();
            list.add("aa");
            list.add("bb");
            list.add("cc");
        }
    }
    
    
jdk的元注解
  • 元注解的基本介绍

    元注解的元Annotation用于修饰其他Annotation

    元注解:本身作用不大

  • 元注解的种类

    1. Retention//指定注解的作用范围 三种SOURCE,CLASS RUNTIME

      • 只能用于修饰一个A弄弄tation定义,用于指定该Annotation可以保留多长时间
        • @Retention包含一个 RetentionPolicy类型的成员变量,使用@Retention时必须为该value成员变量指定值
        • @Retention的三种值
          • @RetentionPolicy.SOURCE:编译器使用后,直接丢弃这种策略的注释
          • @RetentionPolicy.CLASS:编译器将吧主是记录在class文件中,当运行java程序时,jvm不会保留注解。这是默认值
          • @RetentionPolicy.RUNTIME:编译器将把注释记录在class文件中,当运行java程序时,JVM会保留注释,程序可以通过反射获取该注释
    2. Target //指定注解可以在那些地方使用

      • 用于修饰Annotation定义,用于指定被修饰的Annotation能用于修饰那些程序元素。@Target也包含一个名为value的成员变量
    3. Documented //指定该注解是否会在javadoc体现

      用于指定被该元Annotation修饰的Annotation类将被javadoc工具提取成文档,即在生成文档时,可以看到该注释

      说明:定义为Documented的注解必须设置Retention值为RUNTIME

    4. Inherited //子类会继承父类注解

      • 被它修饰的Annotation将具有继承性。如果某个类使用了被@Inherited 修饰的Annotation,则其子类将自动具有该注解
  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值