`
hubin4
  • 浏览: 92957 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

java 1.5 annotation-3 设值让反射拿

    博客分类:
  • JAVA
阅读更多
java 1.5 annotation 的use

前面的什么都没有说,只是介绍基本知识
现在说说用途

注解里可以加属性
反射后可以得到

----------------------
擦播一下
每举和注解都是特殊的类
不可以new
创建每举就是往里加元素
创建注解@加标记
AnnoDemo的注解
public @interface AnnoDemo{
String value();
}
---------------------
@Target({ElementType.METHOD,ElementType.TYPE})
这里肯定看不懂了
有两个知识点
一,数组中只有一个元素时
{}可以不写,有两个了,肯定要写了

还有Target里,肯定有个属性叫value
★而且,还是数组
----------------

-----看看code吧
---------------------------
package com.ncs;

public class EnumWithAbstractMethod {

	public static void main(String[] args) {
		TrafficLights trafficLights = TrafficLights.GREEN;
		System.out.println(trafficLights.time); //30
		trafficLights = TrafficLights.YELLOW;
		System.out.println(trafficLights.time); //3
	}
	
	public enum TrafficLights{
		RED(30){
			public TrafficLights nextLight(){
				return GREEN;
			}
		}
		,GREEN(30){
			public TrafficLights nextLight(){
				return YELLOW;
			}
		},
		YELLOW(3){
			public TrafficLights nextLight(){
				return RED;
			}
		};
		//每个灯都有下一个灯的方法,
		//我们用 抽象 方法
		public abstract TrafficLights nextLight();
		//上面这行不行的,需要你里面的 constants 实现 方法
		//所以在RED后面的一对{}表示一个子类的实现,子类的名字不知道
		
		private int time;
		//构造方法只可以是private
		//然后上面出错了 修改 加() 就完全OK了
		private TrafficLights(int time){this.time = time;}
	}

}


--------------------------------------

package com.ncs.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import com.ncs.EnumWithAbstractMethod;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
public @interface MyAnnotationDemo {
	//就把这个注解 用到 AnnotationDemo上看看再说吧
	String color() default "green";
	//类似接口,public 的 抽象的
	String value();
	
	int[] arr() default {2,5}; 
	//@SuppressWarnings("deprecation")像这个,
	//实际上的指定了一个特殊的属性 value
	//那你就可以省略,直接写 字符串
	//但是仅仅这样也不行,color还没有
	//所以 还可以 指定默认值
	//String color() default "green";
	//这样 compiler就让你过了
	
	//下面玩高级些
	//每举一下,用前面写的每举
	//默认有个红灯 我等下 打印 NEXT 灯, 看看是什么灯出来了
	EnumWithAbstractMethod.TrafficLights light() default  EnumWithAbstractMethod.TrafficLights.RED;
	
	//当然 注解里还可以有注解
	//这自己好好弄弄吧,慢慢理解 
	//详细的东西可以
	//去:The Java Language Specification,Third Edition
	
}


-----------------------------


package com.ncs.annotation;

@MyAnnotationDemo(color="red",value="var",arr={1,2,3})
public class AnnotationDemo {

	@SuppressWarnings("deprecation")
	public static void main(String[] args) {
		System.runFinalizersOnExit(true);
		//在ECLIPSE里会画删除线,提示方法过时
		//其实在是告诉javac
		
		//写了@SuppressWarnings("deprecation")
		//就不警告了
	}
	
	@Deprecated
	public static void sayHi() {
		//总有一天这个方法要过时,
		//但是你不敢删除,
		//因为N多人在用,你一删人家怎么 compile 呢?
		//但是你又想告诉人家 ,这方法过时了,怎么办??
		//注解,
		System.out.println("hello,zxx");
	}
}



----------------------------


package com.ncs.annotation;

public class MyAnnoTest {

	public static void main(String[] args) {
		//先判断 某个类 有没有 某个 注解
		if(AnnotationDemo.class.isAnnotationPresent(MyAnnotationDemo.class)) {
			//cun zai  de 
			MyAnnotationDemo myAnnotationDemo = (MyAnnotationDemo)AnnotationDemo.class.getAnnotation(MyAnnotationDemo.class);
			System.out.println(myAnnotationDemo.color());//output red
			System.out.println(myAnnotationDemo.value());
			System.out.println(myAnnotationDemo.arr().length);
			
			/*
			 * red
				var
				3
			 */
			//虽然都没有写,但是有默认值,就出来 绿灯了
			System.out.println(myAnnotationDemo.light().nextLight().name()); //out GREEN
		}
	}

}


-------------------
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics