java循环注解
我想用注释创建树结构,
@Retention(RetentionPolicy.RUNTIME)
public @interface MyNode {
String name();
MyNode next() default null;
}
但编译器告诉它是循环,因此不允许。
我想知道为什么这是不允许的以及我怎样才能制作类似的东西?
I want to create tree structure with annotation
@Retention(RetentionPolicy.RUNTIME)
public @interface MyNode {
String name();
MyNode next() default null;
}
but compiler tells that it is cycle and hence it is not allowed.
I wonder why it is not allowed and how can I make something like it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它不是树结构,只是线性列表。尝试使用数组来简化声明。
并换行:
现在,只需声明为数组:
It is not a tree structure, just the linear list. Try to use array to simplify declaration.
And wrap:
Now, just declare as array:
有趣的是:Java 语言规范中有一部分似乎与此相矛盾:
但我收到编译错误对于 this (注释引用本身):
和 this (两个注释相互引用):
The funny thing is: there is a part of the Java Language specification that seems to contradict this:
But I get a compile error for both this (annotation references itself):
and this (two annotations reference each other):
如果您确实需要此功能,您可能需要使用 Sun Java 5 编译器,该编译器不强制执行此限制。
但也许更好的解决方案是将节点存储在数组中,并让每个节点都有其子节点的索引。
If you really need this, you may want to use the Sun Java 5 compiler, where this restriction is not enforced.
But maybe a better solution is to store the nodes in an array and let each node have the index(es) of it's child node(s).