basic
ENGLISH: http://www.codeproject.com/Articles/272736/Understanding-Annotations-in-Java
中文版 引用 http://www.blogjava.net/weidagang2046/articles/27958.html
eclipse下使用APT http://www.eclipse.org/jdt/apt/introToAPT.html
eclipse使用APT生成代码 http://code.google.com/p/acris/wiki/AnnotationProcessing_Eclipse
The Inherited Annotation
This is a bit of a complex annotation type. It indicates that the annotated class with this type is automatically inherited. More specifically, if you define an annotation with the @Inherited tag, then annotate a class with your annotation, and finally extend the class in a subclass, all properties of the parent class will be inherited into its subclass. With Example 7, you will get an idea about the benefits of using the @Inherited tag.
Java Annotation Example 7
First, define your annotation:
@Inherited
public @interface myParentObject {
boolean isInherited() default true;
String doSomething() default "Do what?";
}
Next, annotate a class with your annotation:
@myParentObject
public Class myChildObject {
}
As you can see, you do not have to define the interface methods inside the implementing class. These are automatically inherited because of using the @Inherited tag. What would happen if you define the implementing class in old-fashioned Java-style? Take a look at this—defining the implementation class in an old-style-java way:
public class myChildObject implements myParentObject {
public boolean isInherited() {
return false;
}
public String doSomething() {
return "";
}
public boolean equals(Object obj) {
return false;
}
public int hashCode() {
return 0;
}
public String toString() {
return "";
}
public Class annotationType() {
return null;
}
}
Do you see the difference? You can see that you will have to implement all the methods that the parent interface owns. Besides the isInherited() and doSomething() methods from myParentObject, you will have to implement the equals(), toString(), and hasCode() methods of java.lang.Object and also the annotationType() method of java.lang.annotation.Annotation class. It does not matter whether you want to implement these methods or not; you will have to include these in your inherited object.
本文详细介绍了Java中@Inherited注解的使用方法及其带来的便利。通过实例展示如何定义带有@Inherited特性的注解,并在子类中自动继承父类的注解属性,避免了传统Java编程中重复实现接口方法的问题。
473

被折叠的 条评论
为什么被折叠?



