注解的作用之所以那么強(qiáng)大,就是因?yàn)樗?font color="#ff0000">屬性
哪個(gè)班的學(xué)生,這個(gè)時(shí)候可以為胸牌在增加一個(gè)屬性進(jìn)行區(qū)分。加了屬性的標(biāo)記效果為:@MyAnnotation(color=”red”)
MyAnnotation a=(MyAnnotation)AnnotationTest.class.getAnnotation(MyAnnotation.class);System.out.PRintln(a.color());
可以認(rèn)為上面這個(gè)@MyAnnotation是MyAnnotation類的一個(gè)實(shí)例對象.
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;/** * 定義注解 * @author liujl * *///Retention默認(rèn)為class階段,注解MyAnnotation的生命周期為運(yùn)行階段@Retention(RetentionPolicy.RUNTIME)//默認(rèn)值為任何元素,若在數(shù)組里只填寫METHOD則使用注解的類編譯報(bào)錯(cuò)@Target({ElementType.METHOD,ElementType.TYPE})public @interface MyAnnotation { String value();//牛逼的名稱,居然可以在@時(shí)默認(rèn)不寫名字 String color() default "blue"; //String類型 int[] arrayAttr() default {1,2,3};//數(shù)組類型 Class clazz() default java.lang.String.class;//字節(jié)碼 EnumTest.TrafficLamp lamp() default EnumTest.TrafficLamp.GREEN; MetaAnnotation metaAnnotation() default @MetaAnnotation("test"); }/** * Rentention * 關(guān)于java程序的三個(gè)階段 * source階段: .java--->編譯--->.class * class階段: .class-->進(jìn)入jvm檢查階段--->字節(jié)碼 * runtime階段: 已經(jīng)通過安全檢查--被調(diào)入內(nèi)存,被視為字節(jié)碼 * */ package com.itcast.day2;import com.itcast.day1.EnumTest;/** * 使用注解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;/** * 用反射的方式得到注解,并打印其屬性 * @author liujl * */public class MyAnnotationTestRun { public static void main(String[] args) { if(MyAnnotationTest.class.isAnnotationPresent(MyAnnotation.class)){ MyAnnotation MyAnnotation=MyAnnotationTest.class.getAnnotation(MyAnnotation.class); //value很牛逼,如果注解中只有它時(shí)可以省略名稱-value,而直接填值"xxx" System.out.println(MyAnnotation.value()); System.out.println(MyAnnotation.color());//String System.out.println(MyAnnotation.clazz().getName());//字節(jié)碼 System.out.println(MyAnnotation.arrayAttr()[0]);//數(shù)組 System.out.println(MyAnnotation.lamp().nextLamp());//枚舉 紅燈下一盞是綠燈 System.out.println(MyAnnotation.metaAnnotation().value());//注解 注解的屬性也是一個(gè)注解 @代表"實(shí)例化"一個(gè)注解 } }}/**運(yùn)行結(jié)果:123456java.lang.Integer333GREEN : 45334455 */ package com.itcast.day1;/** * 注解 MyAnnotation用到的枚舉類型 * @author liujl * */public class EnumTest { public enum TrafficLamp{ //RED,GREEN,YELLOW這些元素都是枚舉TrafficLame的子類的實(shí)例 RED(30){//內(nèi)部類 @Override public TrafficLamp nextLamp() {//實(shí)現(xiàn)抽象方法 return GREEN; } }, GREEN(45){ @Override public TrafficLamp nextLamp() { return YELLOW; } }, YELLOW(5)/*調(diào)用YELLOW子類有參數(shù)構(gòu)造,子類.super(5)調(diào)用了父類TrafficLamp的有參構(gòu)造*/{ @Override public TrafficLamp nextLamp() { return RED; } }; private int time; public abstract TrafficLamp nextLamp();//抽象方法 private TrafficLamp(int time){this.time=time;}//構(gòu)造方法要私有化 @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語言和虛擬機(jī)說明書)http://docs.Oracle.com/javase/specs/index.htmljava1.5說明書 http://docs.oracle.com/javase/specs/jls/se5.0/html/j3TOC.html
打開后,搜索 Annotation Types ,就可以知道關(guān)于注解的詳細(xì)介紹。
新聞熱點(diǎn)
疑難解答
圖片精選