32、学习 Java 中的注解(参照官方教程)_@unused 注解

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新大数据全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img

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

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

如果你需要这些资料,可以添加V获取:vip204888 (备注大数据)
img

正文

一、注解概述(Annotation)

(1) 大概是什么

📖 Annotations, a form of metadata(元数据), provide data about a program that is not part of the program itself. Annotations have no direct effect on the operation of the code they annotate.
📝 注解是元数据的一种形式,用于提供与程序相关的数据,但这些数据并不是程序的一部分。注解对它们所注释的代码的操作没有直接影响。


(2) 注解的用处

📖① Annotations can be used by the compiler to detect errors or suppress warnings.
📝 注解可以为编译器使用,用于检测错误或抑制警告

📖② 【Compile-time and deployment-time processing】Software tools can process annotation information to generate code, XML files, and so forth.
📝【运行时和部署的时候】软件工具可以通过处理注解信息生成 Java 代码、生成 XML 文件等等 …

📖③【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(); // 审核者(可以写数组)
}

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

使用自定义注解:

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注大数据)
img

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

种语法而已,记住就好。

使用自定义注解:

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注大数据)
[外链图片转存中…(img-8ZzoFUjb-1713691410506)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 16
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值