The role of annotations is so powerful because it has properties
- Annotations are much like interfaces, and properties are much like methods.
- What is the attribute of the annotation
- An annotation is equivalent to a badge, and if you have a badge on your chest, it's a student who is a smart podcast, otherwise it's not. If you want to distinguish between an incident and a podcast,
Which class of students, this time can be added to the badge in a property to distinguish. Tagged effect with attributes: @MyAnnotation (color= "Red")
- Define the properties and application properties of the base type:
- Adds a string color () to the annotation class, or, in fact, the default is the same as the interface, which is the public final
- @MyAnnotation (color= "Red")
- Once the annotation corresponding instance object is obtained by the reflection method, the corresponding method of the property is called by the object.
Myannotation a= (myannotation) annotationtest. class. getannotation (myannotation. Class); System.out.println (A.color ());
It can be considered that the above @myannotation is an instance object of the Myannotation class.
- Specifying a default value for a property
- String color () default "yellow";
- Value property:
- String value () default "Zxx";
- If the annotation has a property named value, and you only want to set the Value property (that is, the other property takes the default value or)
- A case for an annotation definition:
PackageCom.itcast.day2;ImportJava.lang.annotation.ElementType;Importjava.lang.annotation.Retention;ImportJava.lang.annotation.RetentionPolicy;ImportJava.lang.annotation.Target;Importcom.itcast.day1.EnumTest;/*** Define Annotations *@authorLIUJL **///retention default to the class stage, annotated myannotation life cycle is run phase@Retention (retentionpolicy.runtime)//The default value is any element, if only method in the array is used to compile the error with the annotated class@Target ({elementtype.method,elementtype.type}) Public@Interfacemyannotation {String value ();//the name of the cool, incredibly can at @ when the default does not write the nameString Color ()default"Blue";//String Type int[] arrayattr ()default{A-i};//Array TypeClass Clazz ()defaultJava.lang.String.class;//byte codeEnumtest.trafficlamp lamp ()defaultEnumTest.TrafficLamp.GREEN; Metaannotation metaannotation ()default@MetaAnnotation ("Test"); }/*** Rentention * Three stages of the Java program * source stage:. Java---> Compile--->.class * class stage: .class--> into the JVM check stage---> Bytecode * ru Ntime phase: Security has been passed--is transferred into memory and is treated as a byte code **/
Package Com.itcast.day2; Import com.itcast.day1.EnumTest; /** @author*/@MyAnnotation (value= "123", color= "456", arrayattr=333, Clazz=java.lang.integer. class, Lamp=enumtest.trafficlamp.red,[email protected] ("334455") Public Class myannotationtest {}
PackageCom.itcast.day2;/*** Get annotations in a reflective manner and print their properties *@authorLIUJL **/ Public classMyannotationtestrun { Public Static voidMain (string[] args) {if(Myannotationtest.class. isannotationpresent (myannotation.class) {myannotation myannotation=myannotationtest.class. getannotation (myannotation.class); //value is very good, if only in the annotation can omit the name-value, and directly fill the value "XXX"System.out.println (Myannotation.value ()); System.out.println (Myannotation.color ());//StringSystem.out.println (Myannotation.clazz (). GetName ());//byte codeSystem.out.println (myannotation.arrayattr () [0]);//ArraySystem.out.println (Myannotation.lamp (). Nextlamp ());//Enumerate red lights The next one is the green lightSystem.out.println (Myannotation.metaannotation (). value ());//the attribute of the annotation annotation is also an annotation @ representing an annotation of "instantiation" } }}/**running Result: 123456java.lang.integer333green:45334455*/
PackageCom.itcast.day1;/*** Enumeration type used for annotation myannotation *@authorLIUJL **/ Public classEnumtest { Public enumtrafficlamp{//Red,green,yellow These elements are instances of the subclasses of the enumeration TrafficlameRED (30) {//Inner class@Override PublicTrafficlamp Nextlamp () {//implementing an abstract method returnGREEN; }}, GREEN (45) {@Override PublicTrafficlamp Nextlamp () {returnYELLOW; }}, YELLOW (5)/*call the yellow subclass with a parameter construct, subclass. Super (5) called the Trafficlamp of the parent class.*/{@Override PublicTrafficlamp Nextlamp () {returnRED; } }; Private intTime ; Public AbstractTrafficlamp Nextlamp ();//Abstract Methods PrivateTrafficlamp (intTime) { This. Time=time;}//construction method to be privatized@Override PublicString toString () {return This==red? " RED: "+ This. Time: This==green? " GREEN: "+ This. Time: "YELLOW:" + This. Time; } }}
Java Language and Virtual machine specifications (Java language and VM instructions) http://docs.oracle.com/javase/specs/index.html
java1.5 Manual http://docs.oracle.com/javase/specs/jls/se5.0/html/j3TOC.html
Once opened, search Annotation Types to know more about the annotations.
35_ Zhang Xiaoxiang Java High-tech _ add various attributes to annotations