Java-注解

1、内置注解-java.lang包中

  • 重写注解(@Override)
    1、jdk文档介绍
    Override.png

    2、使用范围
    使用范围.png

    3、比如实现toString方法
//@Override-重写注解
    @Override
    public String toString() {
        return super.toString();
    }
  • 已过期注解(@Deprecated)表示方法是不被建议使用的
    1、jdk文档介绍
    Deprecated.png

    2、使用范围
    适用范围.png

    3、注解给一些不建议使用的代码上
    被Deprecated注解后的方法标记.png
  • 镇压警告注解(@SuppressWarnings(参数))
    1、jdk文档介绍
    SuppressWarnings.png

    2、使用范围
    适用范围.png

    3、在有警告的代码上定义,压制警告
    代码实现.png

    4、参数值介绍
    关键字 用途 翻译
    all to suppress all warnings 抑制所有警告
    boxing to suppress warnings relative to boxing/unboxing operations 抑制装箱、拆箱操作时候的警告
    cast to suppress warnings relative to cast operations 抑制映射相关的警告
    dep-ann to suppress warnings relative to deprecated annotation 抑制启用注释的警告
    deprecation to suppress warnings relative to deprecation 抑制过期方法警告
    fallthrough to suppress warnings relative to missing breaks in switch statements 抑制在switch中缺失break的警告
    finally to suppress warnings relative to finally block that don’t return 抑制finally模块没有返回的警告
    hiding to suppress warnings relative to locals that hide variable 抑制相对于隐藏变量的局部变量的警告
    incomplete-switch to suppress warnings relative to missing entries in a switch statement (enum case) 忽略没有完整的switch语句
    nls to suppress warnings relative to non-nls string literals 忽略非nls格式的字符
    null to suppress warnings relative to null analysis 忽略对null的操作
    rawtypes to suppress warnings relative to un-specific types when using generics on class params 使用generics时忽略没有指定相应的类型
    restriction to suppress warnings relative to usage of discouraged or forbidden references 禁止使用与不鼓励或禁止引用相关的警告
    serial to suppress warnings relative to missing serialVersionUID field for a serializable class 忽略在serializable类中没有声明serialVersionUID变量
    static-access to suppress warnings relative to incorrect static access 抑制不正确的静态访问方式警告
    synthetic-access to suppress warnings relative to unoptimized access from inner classes 抑制子类没有按最优方法访问内部类的警告
    unchecked to suppress warnings relative to unchecked operations 抑制没有进行类型检查操作的警告
    unqualified-field-access to suppress warnings relative to field access unqualified 抑制没有权限访问的域的警告
    unused to suppress warnings relative to unused code 抑制没被使用过的代码的警告

2、元注解-java.lang.annotation包中

  • @Target(参数),表示该注解用于什么地方
    1、jdk文档介绍
    Target.png

    2、源码
    源码.png

    3、代码实现
    代码实现.png

    4、Target参数ElementType介绍
    类型 介绍
    ElemenetType.CONSTRUCTOR 构造器声明
    ElemenetType.FIELD 域声明(包括 enum 实例)
    ElemenetType.LOCAL_VARIABLE 局部变量声明
    ElemenetType.METHOD 方法声明
    ElemenetType.PACKAGE 包声明
    ElemenetType.PARAMETER 参数声明
    ElemenetType.TYPE 类,接口(包括注解类型)或enum声明
    ElementType.ANNOTATION_TYPE 注解
  • @Retention(参数),表示在什么级别保存该注解信息
    1、jdk文档介绍
    Retention.png

    2、源码
    源码.png

    3、代码实现
    代码实现.png

    4、Retention参数RetentionPolicy介绍
    类型 介绍
    RetentionPolicy.SOURCE 注解将被编译器丢弃
    RetentionPolicy.CLASS 注解在class文件中可用,但会被VM丢弃
    RetentionPolicy.RUNTIME JVM将在运行期也保留注释,因此可以通过反射机制读取注解的信息。
  • @Documented,表示是否将我们的注解在生成JavaDoc中
    1、JavaDoc百度百科
    2、代码实现
    代码实现.png
  • @Inherited,允许子类继承父类的注解
    1、代码实现
    代码实现.png
  • 元注解总结
    1、@Target:这个注解可以用于什么地方定义
    2、@Retention:JVM运行期间该注解都有效
    3、@Documented:该注解包含在 javadoc 中
    4、@Inherited:该注解允许子类继承

3、自定义注解

定义一个类注解

@Target({ElementType.TYPE})//定义在类上
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation{
   //注解的参数:参数类型 + 参数名();
//    String name() default "";//如果有默认值,在定义注解的时候可以不写参数
   String name();
   int age();
   int id() default -1;//如果默认值为-1,代表不存在。
   String[] schools();
}

定义一个字段注解

@Target({ElementType.FIELD})//定义在字段上
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation2{
    String value();//只有value可以省略参数名
}

通过反射获取注解

@MyAnnotation(name = "wyj",age = 20,schools = {"天狮","南开"})
public class Test03_自定义注解 {
    @MyAnnotation2("namepoliy")
    String name;
    @MyAnnotation2("21")
    String age;
    String schools;
}

class Main{
    public static void main(String[] args) throws ClassNotFoundException {
        // 反射获得 class
        Class clazz = Test03_自定义注解.class;
        // 如果类 Test03_自定义注解 上有注解 @MyAnnotation ,取出注解 value的值
        MyAnnotation declaredAnnotation = (MyAnnotation) clazz.getDeclaredAnnotation(MyAnnotation.class);
        if(declaredAnnotation != null){
           System.out.println("Test03_自定义注解类的注解@MyAnnotation的name值是:"+declaredAnnotation.name());
            System.out.println("Test03_自定义注解类的注解@MyAnnotation的age值是:"+declaredAnnotation.age());
            for (int i = 0; i < declaredAnnotation.schools().length; i++) {
                System.out.println("Test03_自定义注解类的注解@MyAnnotation的schools值是:"+declaredAnnotation.schools()[i]);
            }
        }
    }
}

打印:
Test03_自定义注解类的注解@MyAnnotation的name值是:wyj
Test03_自定义注解类的注解@MyAnnotation的age值是:20
Test03_自定义注解类的注解@MyAnnotation的schools值是:天狮
Test03_自定义注解类的注解@MyAnnotation的schools值是:南开

  • 解释:字段注解就不反射打印了

自定义注解使用场景

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容