35 _ Zhang Xiaoxiang Java high-tech _ adding various attributes for annotation, Zhang Xiaoxiang high-tech
The reason why annotations are so powerful is that they have attributes.
- Annotations are similar to interfaces, and attributes are similar to methods.
- What are annotation attributes?
- An annotation is equivalent to a badge. If you have a badge on your chest, it is the students of the intelligence-based podcast. Otherwise, it is not. If you want to differentiate the problem
In this case, you can add an attribute for the badge to differentiate the students in the class. Added the property Tag: @ MyAnnotation (color = "red ")
- Define the attributes and application attributes of the basic type:
- Add String color () in the annotation class; // The default value is public final, just like the interface.
- @ MyAnnotation (color = "red ")
- Obtain the instance object corresponding to the annotation in reflection mode, and then call the method corresponding to the attribute through this object.
MyAnnotation a=(MyAnnotation)AnnotationTest.class.getAnnotation(MyAnnotation.class);System.out.println(a.color());
The above @ MyAnnotation is an instance object of the MyAnnotation class.
- Specify the default value for the property
- String color () default "yellow ";
- Value Attribute:
- String value () default "zxx ";
- If there is an attribute named value in the annotation, and you only want to set the value attribute (that is, all other attributes use the default value or)
- Example of an annotation definition:
Package com. itcast. day2; import java. lang. annotation. elementType; import java. lang. annotation. retention; import java. lang. annotation. retentionPolicy; import java. lang. annotation. target; import com. itcast. day1.EnumTest;/*** define annotation * @ author liujl ** // The default Retention is the class stage, and the life cycle of annotation MyAnnotation is the run stage @ Retention (RetentionPolicy. RUNTIME) // The default value is any element. If only the METHOD is entered in the array, an error is returned when the annotation class is used for compiling @ Target ({ElementType. METHOD, ElementType. TYPE}) public @ interface MyAnnotation {String value (); // The Name Of niuqiang, which can be left blank by default when "; // String type int [] arrayAttr () default {1, 2, 3}; // array type Class clazz () default java. lang. string. class; // bytecode EnumTest. trafficLamp () default EnumTest. trafficLamp. GREEN; MetaAnnotation metaAnnotation () default @ MetaAnnotation ("test");}/*** constraint ention * Three Phases about java program * source phase :. java ---> compile --->. class * class stage :. class --> enter the jvm check stage ---> bytecode * runtime stage: security check passed-transferred to memory, considered as bytecode **/
Package com. itcast. day2; import com. itcast. day1.EnumTest;/*** use annotation MyAnnotation * @ author hp **/@ MyAnnotation (value = "123", color = "456", arrayAttr = 333, clazz = java. lang. integer. class, lamp = EnumTest. trafficLamp. RED, metaAnnotation = @ MetaAnnotation ("334455") public class MyAnnotationTest {}
Package com. itcast. day2;/*** get the annotation Using Reflection and print its attribute * @ author liujl **/public class MyAnnotationTestRun {public static void main (String [] args) {if (MyAnnotationTest. class. isAnnotationPresent (MyAnnotation. class) {MyAnnotation = MyAnnotationTest. class. getAnnotation (MyAnnotation. class); // value is awesome. If there is only one annotation in it, you can omit the name-value and directly enter the value "xxx" System. out. println (MyAnnotation. value (); System. out. println (MyAnnotation. color (); // String System. out. println (MyAnnotation. clazz (). getName (); // bytecode System. out. println (MyAnnotation. arrayAttr () [0]); // array System. out. println (MyAnnotation. lamp (). nextLamp (); // enumerate the red light. The next light is the green light System. out. println (MyAnnotation. metaAnnotation (). value (); // The annotation attribute is also an annotation @ representing "instantiate" an annotation }}/ ** running result: 123456java. lang. integer333green: 45334455 */
Package com. itcast. day1;/*** annotation: Enumeration type used by MyAnnotation * @ author liujl **/public class EnumTest {public enum TrafficLamp {// RED, GREEN, YELLOW: RED (30) {// internal class @ Override public TrafficLamp nextLamp () {// implement the abstract method return GREEN ;}}, GREEN (45) {@ Override public TrafficLamp nextLamp () {return YELLOW ;}}, YELLOW (5)/* calls the YELLOW subclass to construct parameters, subclass. super (5) calls the parameter construction of the parent class TrafficLamp */{@ Override public TrafficLamp nextLamp () {return RED ;}}; private int time; public abstract TrafficLamp nextLamp (); // The abstract method private TrafficLamp (int time) {this. time = time;} // The constructor must be private @ Override public String toString () {return this = RED? "RED:" + this. time: this = GREEN? "GREEN:" + this. time: "YELLOW:" + this. time ;}}}
Java Language and Virtual Machine Specifications (Java Language and Virtual Machine specification) http://docs.oracle.com/javase/specs/index.html
Java1.5 specification http://docs.oracle.com/javase/specs/jls/se5.0/html/j3TOC.html
Search for Annotation Types to learn more about annotations.