如何在Java中使用@inherited注解?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23973107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use @inherited annotation in Java?
提问by StrugglingCoder
I am not getting the @Inherited
annotation in Java. If it automatically inherits the methods for you then if I need to implement the method in my own way then what about that ?
我没有得到@Inherited
Java 中的注释。如果它自动为您继承方法,那么如果我需要以自己的方式实现该方法,那又如何呢?
How does will it come to know my way of implementation ?
它将如何知道我的实施方式?
Plus it is said if I do not want to use this and do it rather in an old fashioned Java way I have to implement the the equals()
, toString()
, and the hashCode()
methods of the Object
class and also the annotation type method of the java.lang.annotation.Annotation
class.
再加上它是说,如果我不想用这个,做它,而在一个老式的Java办法,我必须实现的equals()
,toString()
以及hashCode()
该方法的Object
类,也是的注释类型的方法java.lang.annotation.Annotation
类。
Why is that?
这是为什么?
I have never implemented those even when I did not know about the @Inherited
annotation and the programs used to work fine also .
即使我不知道@Inherited
注释和程序过去也能正常工作,我也从未实现过这些。
Please somebody explain me from the scratch about this.
请有人从头开始解释我。
采纳答案by fabian
Just that there is no misunderstanding: You do ask about java.lang.annotation.Inherited. This is a annotation for annotations.It means that subclasses of annotated classes are considered having the same annotation as their superclass.
只是没有误解:您确实询问了java.lang.annotation.Inherited。这是注解的注解。这意味着被注解的类的子类被认为与它们的超类具有相同的注解。
Example
例子
Consider the following 2 Annotations:
考虑以下 2 个注解:
@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface InheritedAnnotationType {
}
and
和
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface UninheritedAnnotationType {
}
If three classes are annotated like this:
如果三个类是这样注释的:
@UninheritedAnnotationType
class A {
}
@InheritedAnnotationType
class B extends A {
}
class C extends B {
}
running this code
运行此代码
System.out.println(new A().getClass().getAnnotation(InheritedAnnotationType.class));
System.out.println(new B().getClass().getAnnotation(InheritedAnnotationType.class));
System.out.println(new C().getClass().getAnnotation(InheritedAnnotationType.class));
System.out.println("_________________________________");
System.out.println(new A().getClass().getAnnotation(UninheritedAnnotationType.class));
System.out.println(new B().getClass().getAnnotation(UninheritedAnnotationType.class));
System.out.println(new C().getClass().getAnnotation(UninheritedAnnotationType.class));
will print a result similar to this (depending on the packages of the annotation):
将打印类似于此的结果(取决于注释的包):
null
@InheritedAnnotationType()
@InheritedAnnotationType()
_________________________________
@UninheritedAnnotationType()
null
null
As you can see UninheritedAnnotationType
is not inherited but C
inherits annotation InheritedAnnotationType
from B
.
正如你所看到的UninheritedAnnotationType
是不能继承,但C
继承注释InheritedAnnotationType
从B
。
I don't know what methods have to do with that.
我不知道这与什么方法有关。