有什么样的Java注释继承? [英] Is there something like Annotation Inheritance in java?

查看:85
本文介绍了有什么样的Java注释继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我探索的注解,来到一个点,一些注解似乎有其中的一个层次。

I'm exploring annotations and came to a point where some annotations seems to have a hierarchy among them.

我使用注释来产生背景为卡code。有不同的卡种(因而不同的code和注释),但有一些是常见当中就像一个名称的某些元素。

I'm using annotations to generate code in the background for Cards. There are different Card types (thus different code and annotations) but there are certain elements that are common among them like a name.

@Target(value = {ElementType.TYPE})
public @interface Move extends Page{
 String method1();
 String method2();
}

和这将是常见的注释:

@Target(value = {ElementType.TYPE})
public @interface Page{
 String method3();
}

在上面的例子中我希望移动继承method3但我得到扩展不与标注有效的警告说法。我是想有一个注释扩展一个共同的基础之一,但不起作用。是,甚至有可能还是仅仅是一个设计问题?

In the example above I would expect Move to inherit method3 but I get a warning saying that extends is not valid with annotations. I was trying to have an Annotation extends a common base one but that doesn't work. Is that even possible or is just a design issue?

推荐答案

不幸的是,没有。显然,它是与上一类阅读注释不加载这些一路程序。见<一href=\"http://stackoverflow.com/questions/1624084/why-is-not-possible-to-extend-annotations-in-java\">Why是不可能在Java扩展注解?

Unfortunately, no. Apparently it has something to do with programs that read the annotations on a class without loading them all the way. See Why is not possible to extend annotations in Java?

不过,类型也继承其超类的标注,如果这些注解<一个href=\"http://download.oracle.com/javase/1,5,0/docs/api/java/lang/annotation/Inherited.html\"><$c$c>@Inherited.

However, types do inherit the annotations of their superclass if those annotations are @Inherited.

另外,除非你需要这些方法来进行互动,你可以只堆栈上的注释类:

Also, unless you need those methods to interact, you could just stack the annotations on your class:

@Move
@Page
public class myAwesomeClass {}

有一些原因,不会为你工作?

Is there some reason that wouldn't work for you?

这篇关于有什么样的Java注释继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆