0
在下面的代碼打印註釋繼承作用怪異
@MyTestAnnotation():MyTestAnnotation是否被指定爲@Inherited或不FOO
兩個類B和C的。 我認爲只有在@Inherited被指定的情況下才會繼承註釋,並且默認的註釋繼承行爲是它們沒有被繼承。這裏發生了什麼事?
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
public class AnnotationTest {
public static void main(String[] args) {
B b = new B();
C c = new C();
printMethdodsAndAnotations(b);
printMethdodsAndAnotations(c);
}
public static void printMethdodsAndAnotations(Object obj) {
Method[] methods = obj.getClass().getMethods();
for (int i = 0; i < methods.length; i++) {
Annotation[] annotations = methods[i].getAnnotations();
if (annotations.length > 0)
{
for (int j = 0; j < annotations.length; j++) {
System.out.println(annotations[j].toString() + ":" + methods[i].getName());
}
}
}
}
}
//@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface MyTestAnnotation {
}
abstract class A {
public abstract void foo();
}
class B extends A {
@MyTestAnnotation
public void foo() {
System.out.println("B's foo()");
}
}
class C extends B {
public void foo2() {
System.out.println("C's foo()");
}
}
繼承的註釋只適用於類,而不適用於方法。在這兩種情況下,您都顯示相同foo方法的註釋,因此邏輯上註釋是相同的。繼承的javadoc明確指出:'請注意,如果註釋類型用於註釋除類之外的其他任何內容,則此元註釋類型不起作用。 「 –