pet*_*bel 7 java annotations block
是否可以注释一段代码?例如循环或简单的花括号?如果是这样,怎么样?
First.java
package An;
import An.ForCycle;
class First {
public static void main(String[] args) {
First f = new First();
}
public First () {
@ForCycle
{ // error: illegal start of type {
int k;
}
@ForCycle
for (int i = 0; i < 5; i++) { // similar error (illegal start...)
System.out.println(i);
}
}
}
Run Code Online (Sandbox Code Playgroud)
ForCycle.java
package An;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.SOURCE)
public @interface ForCycle {}
Run Code Online (Sandbox Code Playgroud)
根据http://www.javacodegeeks.com/2012/11/java-annotations-tutorial-with-custom-annotation.html
@Target - 表示注释类型适用的程序元素的种类.一些可能的值是TYPE,METHOD,CONSTRUCTOR,FIELD等.如果不存在Target元注释,则可以在任何程序元素上使用注释.
任何程序元素(我猜)也意味着阻止,不是吗?那么,为什么我不能注释块或为?我错过了什么?
感谢帮助
不,你不能这样做.其实你已经自己回答了你的问题.有一个关闭的目标列表,可以使用注释:{TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE}
.你可以看到没有"阻止"这样的东西.为什么?可能是因为注释必须附加到命名的内容,例如方法,字段,类等.
确实想想如何使用注释?使用反射API访问注释.您可以请求特定方法或字段的注释.您期望如何请求块的注释?
最后一个通知.注释可以在运行时或编译时使用.编译器时可用的注释由编译器,IDE和注释处理器使用,以生成其他警告,错误或代码.IntelliJ作为一个众所周知的IDE,具有许多静态代码分析功能,支持所谓的"块注释",在语法上是常规的内联注释,但被IDE视为可以抑制某些警告的注释.
归档时间: |
|
查看次数: |
2891 次 |
最近记录: |