java注解

本文详细介绍了Java中的三种基本注解:@Override用于检查方法重写,@Deprecated标记过时元素,@SuppressWarnings抑制编译警告。阐述了它们的使用方法、源码分析以及常见应用场景。

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

一、基本介绍
       

1.概述 : 


                java注解(Annotation)[ˌ ænəˈ teɪʃn],又称java标注,也被称为元数据(关于数据的数据,描述数据的数据)(Metadata)[ˈ metədeɪtə],可用于修饰或者解释包、类、方法、属性、构造器,局部变量等数据信息。

                java注解和注释一样,不会影响程序逻辑,但注解可以被编译或运行,相当于嵌入在代码中的补充信息。在javaSE中,注解的功能比较单一,例如标记过时的功能,忽略警告等等。但注解在javaEE中占据了更重要的角色,尤其是在使用框架时,例如用来配置应用程序的任何切面,代替javaEE旧版中所残留的冗余代码和XML配置等等。

2.使用 : 


                使用Annotation时,要在它前面增加“@”符号,并把该注解当作一个修饰符来使用。以修饰它支持的程序元素。


二、3种基本注解

   0.总览 :

        @Override - 检查该方法是否是重写方法。如果发现其父类,或者是引用的接口中并没有该方法时,会报编译错误。(强制语法校验)

        @Deprecated - 标记某个程序元素(类或者方法等)已过时。如果使用该方法,会报编译警告。

        @SuppressWarnings - 指示编译器去忽略注解中声明的警告。

1.“@Override”:

@Override最大的价值在于它的判断功能。通过Ctrl + b/B 可以查看@Override源码,如下 : 


package java.lang;
 
import java.lang.annotation.*;
 
/**
 * Indicates that a method declaration is intended to override a
 * method declaration in a supertype. If a method is annotated with
 * this annotation type compilers are required to generate an error
 * message unless at least one of the following conditions hold:
 *
 * <ul><li>
 * The method does override or implement a method declared in a
 * supertype.
 * </li><li>
 * The method has a signature that is override-equivalent to that of
 * any public method declared in {@linkplain Object}.
 * </li></ul>
 *
 * @author  Peter von der Ah&eacute;
 * @author  Joshua Bloch
 * @jls 8.4.8 Inheritance, Overriding, and Hiding
 * @jls 9.4.1 Inheritance and Overriding
 * @jls 9.6.4.4 @Override
 * @since 1.5
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}

  其实,@Override源码中,最重要的无非最后两行。“@interface”表示Override是一个注解类,而不是接口。
                “@Override”只能修饰方法,不能修饰其他程序元素(比如类,包,属性等)。这一点,其实源码中也有体现——Override源码中的“@Target(ElementType.METHOD)”明确指出了修饰的元素类型是METHOD(方法)。

                PS : “@Target”是用于修饰注解的注解,我们称之为“元注解”


2.“@Deprecated”:

①含义

Deprecated就是不赞成、不建议使用的意思

 ②源码

                “@Deprecated”可以标记过时的程序元素。仍然通过Ctrl + b/B 快捷键追溯一下Deprecated的源码,如下 : (这次仅截取关键部分)

同样,“@interface”表示Deprecated是一个注解类而“@Target”元注解中,也标注出了@Deprecated可以修饰的数据元素,从左往右依次为 : {构造器,属性,局部变量,方法,包,模块,参数,类型(即类)}。

③使用

“@Deprecated”修饰的程序元素并非无法使用,只是表示过时了,不建议使用,不是不可以使用。来举个栗子

 
package advanced.Annotation;
 
/**
 * @author : Cyan_RA9
 * @version : 5.0
 */
public class Deprecated_ {
    @Deprecated
    public void f() {
        System.out.println("这方法没啥屁用,不建议使用。你非要用当我没说。。。");
    }
}
 
class Test {
    public static void main(String[] args) {
        Deprecated_ deprecated_ = new Deprecated_();
        deprecated_.f();
    }
}

   其实,当你在调用@Deprecated注解修饰的方法时,IDEA就会给出提示,如下图所示 :

可以明显看到被“@Deprecated”注解修饰的f() 方法是被删除线“ ”标注的。

  ④补充

啥时候用“@Deprecated”比较多?

比如,当JDK进行版本更迭的时候,新版本的JDK对旧版本的某些类或者某些方法进行了更新,可能会定义新的类型和功能,这时候就会在旧版本的对应类型或者对应方法前打上@Deprecated标注,提醒java人们这是旧的啦,建议你用新的捏。

  3.“@SuppressWarnings” :

  ①含义

  SuppressWarnings就是抑制警告,禁止警告的意思

  ②格式

 “@SuppressWarnings”的使用与前面两个注解略有出入。使用格式如下 :

 
@SuppressWarnings("explanation1", "explanation2", "explanation3", ......)
//双引号中的说明信息不同,达到的抑制警告效果便不同。

  ③属性说明汇总

 “@SuppressWarnings”中常见的属性说明有 :

⑤作用域


                关于@SuppressWarnings注解的作用域 : 取决于该注解的定义位置。

                eg : 当它放在main方法前时,抑制警告的范围就是main函数;当它定义在类上,作用域就是整个类;当然,也可以在固定的警告语句上面使用@SuppressWarnings注解,通过传入指定的属性说明实现精准的消除警告。

 ⑥源码


通过Ctrl + b/B快捷键快速追溯到SuppressWarnings注解类的源码,源码如下 

 
package java.lang;
 
import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*;
 
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, MODULE})
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
    String[] value();
}

同样的。"@interface"元注解表明SuppressWarnings是一个注解类。而“@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, MODULE})”表明了@SuppressWarnings注解的使用范围,从左往后依次是"{类型(类),属性,方法,参数,构造器,局部变量,模块}"。


 

 
package java.lang;
 
import java.lang.annotation.*;
 
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}

 

 
package java.lang.annotation;
 
public enum ElementType {
    /** Class, interface (including annotation interface), enum, or record
     * declaration */
    TYPE,
 
    /** Field declaration (includes enum constants) */
    FIELD,
 
    /** Method declaration */
    METHOD,
 
    /** Formal parameter declaration */
    PARAMETER,
 
    /** Constructor declaration */
    CONSTRUCTOR,
 
    /** Local variable declaration */
    LOCAL_VARIABLE,
 
    /** Annotation interface declaration (Formerly known as an annotation type.) */
    ANNOTATION_TYPE,
 
    /** Package declaration */
    PACKAGE,
 
    /**
     * Type parameter declaration
     *
     * @since 1.8
     */
    TYPE_PARAMETER,
 
    /**
     * Use of a type
     *
     * @since 1.8
     */
    TYPE_USE,
 
    /**
     * Module declaration.
     *
     * @since 9
     */
    MODULE,
 
    /**
     * Record component
     *
     * @jls 8.10.3 Record Members
     * @jls 9.7.4 Where Annotations May Appear
     *
     * @since 16
     */
    RECORD_COMPONENT;
}

然后在定义一个测试类Test_EX,并在测试类中定义两个方法,用自定义的注解”@Demo“去修饰方法。如下 :

 
package advanced.Annotation.test;
 
import advanced.Annotation.Demo;
 
/**
 * @author : Cyan_RA9
 * @version : 1.0
 */
class Test_EX {
    @Demo
    public void greet() {
        System.out.println("Hello!");
    }
 
    @Demo
    public void eat() {
        System.out.println("美汁儿美汁儿~");
    }
}

 

 

最后,找到目录下的index.html文件,点击打开就能看到API文档。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值