gpt4 book ai didi

java - 关于@interface注解及其实现

转载 作者:太空宇宙 更新时间:2023-11-04 06:54:53 24 4
gpt4 key购买 nike

我正在寻找@interface的实现。但似乎 @interface 不需要实现。

这些注释只是携带供编译器检查的信息。编译器会将这些信息放入类文件中。然后我们使用字节码阅读器或反射API来读取这些信息返回。

我的理解正确吗?

最佳答案

I'm looking for the implementation of @interface. But it seems the @interface don't need an implementation.

如果您的字面意思是语言构造@interface,那么您应该注意它本身不是注释,它只是看起来很相似。它是定义注释类型时使用的关键字。它实际上是两个标记,@interface,它们之间可以有空格。请参阅:Java 7 Language Specification - 9.6. Annotation Types

Those annotations just carry information for compiler's checking. and the compiler will put those information to class file. then we use byte code reader or reflection API to read those information back.

正确,注释只是元数据,它们本身从不做任何事情。可见性取决于Retention元注释。根据保留策略,可以使用反射使它们对运行时可见或限制为源代码,因此它们不会包含在字节码中。最常见的方法可能是使用反射,另一种方法是在编译之前使用注释处理器

关于java - 关于@interface注解及其实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22888620/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com