2024年最新32、学习 Java 中的注解(参照官方教程)_@unused 注解(3),附架构师必备技术详解

img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化资料的朋友,可以戳这里获取

📖③【Runtime processing】Some annotations are available to be examined at runtime.
📝 有些注解可在运行时被检测到

二、注解基础

(1) 注解的基本格式

📖 In its simplest form, an annotation looks like the following:
📝 一个注解最简单的格式如下所示:

@Entity


📖 The at sign character (@) indicates to the compiler that what follows is an annotation. In the following example, the annotation’s name is Override:
📝 @ 符号用于告诉编译器 @ 符号后面的内容是一个注解。在下面的例子中,注解的名字是:Override

class Dog extends Animal {
    @Override
    public void test() {
        super.test();
    }
}


📖 The annotation can include elements, which can be named or unnamed, and there are values for those elements:
📝 注解可以包含元素。元素可以有名字,也可以没有名字。元素如下所示:

@Author(
        name = "庆医",
        date = "2022/5/20"
)
class CommonClass { 
    
}

🌼 上面代码中的 name 和 date 是注解的元素
🌼【庆医】和【2022/5/20】是元素的值

@SuppressWarnings(value = "unused")
class Whatever {
    private String s;
}

🌼 上面代码中的 @ 告诉编译器 @ 符号后面的是注解
🌼 SuppressWarnings 是注解名
🌼 value 是 SuppressWarnings 注解的元素
🌼【unused】是 value 元素的值


📖 If there is just one element named value, then the name can be omitted.
📝 如果注解中只写了一个元素,并且元素的名字是 value,那么元素名可以省略掉(如下所示)

//@SuppressWarnings(value = "unused")
@SuppressWarnings("unused")
class Whatever {
    private String s;
}


📖 If the annotation has no elements, then the parentheses(圆括号) can be omitted.
📝 如果注解中没有元素,那么注解的圆括号可以省略掉(如下所示)

class Dog extends Animal {
    @Override // Override 注解没有元素, 圆括号可以省略
    public void test() {
        super.test();
    }
}


📖 It is also possible to use multiple annotations on the same declaration.
📝 也可以在同一个声明上使用多个注解

class Dog extends Animal {

    @Override
    @SuppressWarnings("unused")
    public void test() {
        int a = 66;
        super.test();
    }

}


📖 The annotation type can be one of the types that are defined in the java.lang or java.lang.annotation packages of the Java SE API. It is also possible to define your own annotation type.
📝 注解类型可能存在于 Java 标准版的 java.lang 包或java.lang.annotation 包中。您也可以定义自己的注解类型。

(2) 注解可以使用在哪儿

📖 Annotations can be applied to declarations: declarations of classes, fields, methods, and other program elements. When used on a declaration, each annotation often appears, by convention, on its own line.
📝 注解可以运用于声明:类声明、字段声明、方法声明和其他程序元素的声明。当多个注解使用在同一个声明的时候,每一个注解独占一行。
在这里插入图片描述

三、创建注解类型

📖 Many annotations replace comments in code.
📝 很多注解取代了代码中的注释


📖 The annotation type definition looks similar to an interface definition where the keyword interface is preceded by the at sign (@).
📝 定义注解类型就像定义一个接口一样。只是接口的关键字(interface)前面增加了 @ 符号【@ 符号是注解类型的标志】

📖 Annotation types are a form of interface.【注解类型是接口的一种形式】

📖 The body of the annotation definition contains annotation type element declarations, which look a lot like methods. Note that they can define optional default values.
📝 注解定义的主体中包含注解类型元素的定义,注解类型元素的定义看起有点像方法(定义注解类型元素的时候可以提供可选的默认值)

创建注解类型示例:

/\*\*
 \* @author 庆医
 \* @describe 创建一个注解类型,
 \* 花括号中可定义注解的元素(且元素可以有可选的默认值)
 \*/
@Documented // 使 DescribeInfo 注解能够在 javadoc 文档中出现
public @interface DescribeInfo {
    /\*
 元素的类型是:String
 元素名是:author
 \*/
    String author(); // 作者

    String date(); // 创建时间

    /\*
 元素的类型是:int
 元素名是:currentRevision
 该元素的默认值是:1
 \*/
    int currentRevision() default 1; // 当前版本

    String lastModifiedDate() default ""; // 最后一次修改的时间

    String lastModifiedBy() default ""; // 最后一次是被誰修改的

    String[] reviewers(); // 审核者(可以写数组)
}

🌼 元素的定义和方法有点像,一种语法而已,记住就好。

使用自定义注解:

@DescribeInfo(
        author = "庆医",
        date = "2022/2/2",
        currentRevision = 3,
        lastModifiedDate = "2022/3/3",
        lastModifiedBy = "庆医儿子",
        reviewers = {"Tom", "Eric"}
)
public class MainTest {
}


📖 To make the information in @DescribeInfo appear in Javadoc-generated documentation, you must annotate the @DescribeInfo definition with the @Documented annotation.

📝 为了能够在 javadoc 文档中显示@DescribeInfo自定义注解的信息,您必须在定义该注解的时候标注@Documented注解。

四、注解介绍

📄 注解(Annotation)也被称为元数据(Metadata),用于解释包、类、方法、属性、构造器、局部变量等数据的信息

📄 和注释一样,注解不影响程序的逻辑。当注解可以被编译或运行(相当于嵌入在代码中的补充信息)

📄 在 JavaSE 中,注解的作用非常简单(例如:标记过时的功能、忽略警告等)

img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化资料的朋友,可以戳这里获取

lwvWuDc-1715295467776)]
[外链图片转存中…(img-Ge5yw8rX-1715295467776)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化资料的朋友,可以戳这里获取

  • 19
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值