@interface-什么?
我知道什么是接口,但我对 java 的了解不够,不知道如何搜索我的答案。那么这是什么意思和作用:
public @interface ThreadSafe { }
// different file
@ThreadSafe
public class Model {
这行“@interface”是什么意思/做什么?当它应用在“@ThreadSafe”上方时,它意味着什么/做什么
I know what interfaces are, but I don't know enough about java to know how to search for my answer. So what does this mean and do:
public @interface ThreadSafe { }
// different file
@ThreadSafe
public class Model {
What does this line mean/do "@interface"? What does it mean/do when it's applied above "@ThreadSafe"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这称为注释。您可能想查看 Java 教程。它对它们是什么以及如何使用有很好的解释。
https://docs.oracle.com/javase/tutorial/java/注释/index.html
This is called an annotation. You may want to look at the Java tutorial. It has a pretty good explanation of what they are and how they are used.
https://docs.oracle.com/javase/tutorial/java/annotations/index.html
许多注释取代了代码中的注释。
假设软件组传统上以提供重要信息的注释开始每个类的主体:
要使用注释添加相同的元数据,必须首先定义注释类型。执行此操作的语法为:
注释类型定义看起来类似于接口定义,其中关键字接口前面带有 at 符号 (@)(@ = AT,如注释类型中一样)。注释类型是一种界面形式,将在后面的课程中介绍。目前,您不需要了解接口。
前面的注释定义的主体包含注释类型元素声明,它看起来很像方法。请注意,它们可以定义可选的默认值。
定义注释类型后,您可以使用该类型的注释,并填写值,如下所示:
Source: https://docs.oracle.com/javase/tutorial/java/annotations/declaring.html
Many annotations replace comments in code.
Suppose that a software group traditionally starts the body of every class with comments providing important information:
To add this same metadata with an annotation, you must first define the annotation type. The syntax for doing this is:
The annotation type definition looks similar to an interface definition where the keyword interface is preceded by the at sign (@) (@ = AT, as in annotation type). Annotation types are a form of interface, which will be covered in a later lesson. For the moment, you do not need to understand interfaces.
The body of the previous annotation definition contains annotation type element declarations, which look a lot like methods. Note that they can define optional default values.
After the annotation type is defined, you can use annotations of that type, with the values filled in, like this:
Source: https://docs.oracle.com/javase/tutorial/java/annotations/declaring.html