java自定义注解学习(二)_注解详解

本文深入探讨Java注解的使用,包括内置注解如@Override、@Deprecated和@SuppressWarnings的功能与应用场景,以及元注解@Target、@Retention、@Documented和@Inherited的详细解释。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上篇文章,我们简单的实现了一个自定义注解,相信大家对自定义注解有了个简单的认识,这篇,这样介绍下注解中的元注解和内置注解

整体图示

o_%e6%b3%a8%e8%a7%a3%e5%88%86%e7%b1%bb.png

内置注解

@Override 重写覆盖

这个注解大家应该经常用到,主要在子类重写父类的方法,比如toString()方法

package com.kevin.demo;

public class Demo1 {

    @Override
    public String toString(){
        return "demo1";
    }
}
@Deprecated 过时

@Deprecated可以修饰的范围很广,包括类、方法、字段、参数等,它表示对应的代码已经过时了,程序员不应该使用它,不过,它是一种警告,而不是强制性的。

package com.kevin.demo;

public class Demo1 {

    @Deprecated
    public void goHome(){
        System.out.println("过时的方法");
    }
}

idea中调用这些方法,编译器也会显示删除线并警告

TIM%E6%88%AA%E5%9B%BE20181016113029.png

@SuppressWarning 压制Java的编译警告

@SuppressWarnings表示压制Java的编译警告,它有一个必填参数,表示压制哪种类型的警告.

关键字用途
allto suppress all warnings
boxingto suppress warnings relative to boxing/unboxing operations
castto suppress warnings relative to cast operations
dep-annto suppress warnings relative to deprecated annotation
deprecationto suppress warnings relative to deprecation
fallthroughto suppress warnings relative to missing breaks in switch statements
finallyto suppress warnings relative to finally block that don¡¯t return
hidingto suppress warnings relative to locals that hide variable
incomplete-switchto suppress warnings relative to missing entries in a switch statement (enum case)
nlsto suppress warnings relative to non-nls string literals
nullto suppress warnings relative to null analysis
rawtypesto suppress warnings relative to un-specific types when using generics on class params
restrictionto suppress warnings relative to usage of discouraged or forbidden references
serialto suppress warnings relative to missing serialVersionUID field for a serializable class
static-accessto suppress warnings relative to incorrect static access
synthetic-accessto suppress warnings relative to unoptimized access from inner classes
uncheckedto suppress warnings relative to unchecked operations
unqualified-field-accessto suppress warnings relative to field access unqualified
unusedto suppress warnings relative to unused code

上面的方法,我们就可以增加

    @SuppressWarnings("deprecation")
    public static void main(String[] args) {
        Demo1 demo1 = new Demo1();
        demo1.goHome();
    }

元注解

元注解:注解的注解,即java为注解开发特准备的注解。

我们以上面讲到的java内置注解@Override为例,学习下java元注解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}
@Target

@Target表示注解的目标,@Override的目标是方法(ElementType.METHOD),ElementType是一个枚举,其他可选值有:

  • TYPE:表示类、接口(包括注解),或者枚举声明
  • FIELD:字段,包括枚举常量
  • METHOD:方法
  • PARAMETER:方法中的参数
  • CONSTRUCTOR:构造方法
  • LOCAL_VARIABLE:本地变量
  • ANNOTATION_TYPE:注解类型
  • PACKAGE:包

目标可以有多个,用{}表示,比如@SuppressWarnings@Target就有多个,定义为:

@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
    String[] value();
}

如果没有声明@Target,默认为适用于所有类型。我们上篇文章的demo就没有声明@Target

@Retention

@Retention表示注解信息保留到什么时候,取值只能有一个,类型为RetentionPolicy,它是一个枚举,有三个取值:

  • SOURCE:只在源代码中保留,编译器将代码编译为字节码文件后就会丢掉
  • CLASS:保留到字节码文件中,但Java虚拟机将class文件加载到内存时不一定会在内存中保留
  • RUNTIME:一直保留到运行时

如果没有声明@Retention,默认为CLASS

@Override@SuppressWarnings都是给编译器用的,所以@Retention都是RetentionPolicy.SOURCE

@Documented

用于指定javadoc生成API文档时显示该注解信息。Documented是一个标记注解,没有成员。

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {
}
@Inherited

@Inherited 元注解是一个标记注解,@Inherited阐述了某个被标注的类型是被继承的。

看个栗子

public class Demo1 {

        @Inherited
        @Retention(RetentionPolicy.RUNTIME)
        static @interface Test {
        }

        @Test
        static class Base {
        }

        static class Child extends Base {
        }

        public static void main(String[] args) {
            System.out.println(Child.class.isAnnotationPresent(Test.class));
        }

}

main方法检查Child类是否有Test注解,输出为true,这是因为Test有注解@Inherited,如果去掉,输出就变成false

总结

好了,这篇先学习到这,我要好好看看这些知识,下篇介绍注解的解析啦。好了。玩的开心!

参考

  • 1、https://www.cnblogs.com/fsjohnhuang/p/4040785.html
  • 2、https://www.cnblogs.com/swiftma/p/6838654.html

转载于:https://www.cnblogs.com/zhenghengbin/p/9797668.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值