35_张孝祥Java高新技术_为注解增加各种属性

时间:2015-01-30 15:43:03   收藏:0   阅读:188

注解的作用之所以那么强大,就是因为它有属性

 

哪个班的学生,这个时候可以为胸牌在增加一个属性进行区分。加了属性的标记效果为:@MyAnnotation(color=”red”)

MyAnnotation a=(MyAnnotation)AnnotationTest.class.getAnnotation(MyAnnotation.class);

System.out.println(a.color());

可以认为上面这个@MyAnnotation是MyAnnotation类的一个实例对象.

 

 

 

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默认为class阶段,注解MyAnnotation的生命周期为运行阶段
@Retention(RetentionPolicy.RUNTIME)

//默认值为任何元素,若在数组里只填写METHOD则使用注解的类编译报错
@Target({ElementType.METHOD,ElementType.TYPE})
public @interface MyAnnotation {
    String value();//牛逼的名称,居然可以在@时默认不写名字
    String color() default "blue"; //String类型
    int[] arrayAttr() default {1,2,3};//数组类型 
    Class clazz() default java.lang.String.class;//字节码
    EnumTest.TrafficLamp lamp() default EnumTest.TrafficLamp.GREEN;
    MetaAnnotation metaAnnotation() default @MetaAnnotation("test"); 
    
}

/**
 * Rentention
 * 关于java程序的三个阶段
 * source阶段: .java--->编译--->.class
 * class阶段:  .class-->进入jvm检查阶段--->字节码
 * runtime阶段: 已经通过安全检查--被调入内存,被视为字节码
 * 
 */
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很牛逼,如果注解中只有它时可以省略名称-value,而直接填值"xxx"
            System.out.println(MyAnnotation.value());
            System.out.println(MyAnnotation.color());//String
            System.out.println(MyAnnotation.clazz().getName());//字节码
            System.out.println(MyAnnotation.arrayAttr()[0]);//数组
            System.out.println(MyAnnotation.lamp().nextLamp());//枚举 红灯下一盏是绿灯
            System.out.println(MyAnnotation.metaAnnotation().value());//注解 注解的属性也是一个注解 @代表"实例化"一个注解
            
        }
    }
}
/**
运行结果:
123
456
java.lang.Integer
333
GREEN : 45
334455
 */
package com.itcast.day1;

/**
 * 注解 MyAnnotation用到的枚举类型
 * @author liujl
 *
 */
public class EnumTest {

    public enum TrafficLamp{
        
        //RED,GREEN,YELLOW这些元素都是枚举TrafficLame的子类的实例
        RED(30){//内部类
            @Override
            public TrafficLamp nextLamp() {//实现抽象方法
                return GREEN;
            }
        },
        GREEN(45){
            @Override
            public TrafficLamp nextLamp() {
                return YELLOW;
            }
        },
        YELLOW(5)/*调用YELLOW子类有参数构造,子类.super(5)调用了父类TrafficLamp的有参构造*/{
            @Override
            public TrafficLamp nextLamp() {
                return RED;
            }
        };
        private int time;
        public abstract TrafficLamp nextLamp();//抽象方法
        private TrafficLamp(int time){this.time=time;}//构造方法要私有化
        
        
        @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语言和虚拟机说明书)http://docs.oracle.com/javase/specs/index.html

java1.5说明书  http://docs.oracle.com/javase/specs/jls/se5.0/html/j3TOC.html

打开后,搜索   Annotation Types ,就可以知道关于注解的详细介绍。

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!