java学习笔记(annotation 注解_1)

1. java se 5.0 引入annotation的概念

2. java 中常见的几个注解:

1) @Override 表示子类要重写父类的方法

2)@Deprecated 表示方法是不建议被使用的

3)@SuppressWarnings注解表示压制警告


3. 如何定义自己的Annotaion类型

1) 使用@interface来定义,实际上是自动继承了java.lang.annotation.Annotation接口。

定义时,不定继承其他Annotation形态或接口

public @interface AnnotationTest {

}
2)给其增加成员变量

public @interface AnnotationTest {

	//如果指定的名字是value时,则在应用中赋值时,
	//可以不用显示的写是给value赋值,其他value一外的名字,都必须显示的写出是给谁赋值
	//比如:@AnnotationTest(value1="hello")
	//给value设置默认值default "hello"
	String value() default "hello";	
}

3) 增加一个枚举型的成员变量

public @interface AnnotationTest {

	//给value设置默认值default "hello"
	String value() default "hello";
	
       //在使用时 @AnnotationTest(value2=EnumTest.Hello)
	EnumTest value2();
	
}

enum EnumTest{
	Hello,World,Welcome
}

以上就定义了一个自己的Annotation类型,但没什么实际用处!


4. 用@Retention(java.lang.annotation.RetentionPolicy)注解来修饰自己定义的Annotation类型

@Retention指示编译程序该如何对待你自己定义的Annotation类型。

有三种类型:

1.  SOURCE //编译程序处理完Annotation信息后就完成任务,例如:@SuppressWarnings

2. CLASS,//编译曾需将Annotation储存在Class文件中,是缺省情况

3.RUNTIME//编译程序将Annotation储存于class文件中,可由vm读入


Demo如下:

1) 定义一个自己的Annotation,并使用@Retention来修饰

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

@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {

	String hello() default "sehngsiyuan";
	String world();
}

2) 定义一个测试类MyTest,来使用自己的Annotation类型

@MyAnnotation(hello="beijing",world="shanghai")
public class MyTest {

	@MyAnnotation(hello="hello",world="beijing")
	public void output(){
		System.out.println("Hello world!");
	}
}

3)利用反射机制类来读取测试类MyTest中的注解,学习的重点

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class MyReflection {

	/**
	 * 获得方法上面的注解信息
	 * @param args
	 */
	public static void main(String[] args)throws Exception {
		//得到一个目标类对象
		MyTest myTest = new MyTest();
		
		//得到目标类所对应的Class类的对象
		Class<MyTest> c = MyTest.class;
		//利用反射机制从Class类对象中得到目标类对象的output方法
		Method method = c.getMethod("output", new Class[]{});
		
		//判断目标类对象的output方法 上面是否存在注解MyAnnotation,如果存在返回为true,否则返回false;
		if(method.isAnnotationPresent(MyAnnotation.class)){
			//执行目标类对象的output方法
			method.invoke(myTest, new Object[]{});
			
			//得到方法上面的注解对象MyAnnotation
			MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);
			
			//得到其变量
			String hello = myAnnotation.hello();
			String world = myAnnotation.world();
			
			System.out.println(hello+","+world);
		}
		
		//只会得到RetentionPolicy设置为RUNTIME的注解对象
		Annotation [] annotations = method.getAnnotations();
		
		for(Annotation annotation:annotations){
			
			System.out.println(annotation.annotationType().toString());
		}

	}

}


5. 使用java.lang.annotation.Target可以定义自己定义的Annotation的使用时机

            在定义时要指定java.lang.annotation.ElementType的枚举值之一。

例如:

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

@Target(ElementType.METHOD)//ElementType.Method表示该注解只能修饰方法
public @interface MyTarget {

	String value();
}

以下列出ElementType的多有类型:

TYPE//class,interface,enum

FIELD,//适用field

PARAMETER//适用method上的parameter

CONSTRUCTOR//适用构造方法

LOCAL_VARIABLE//适用局部变量

ANNOTATION_TYPE//适用annotation形态

PACKAGE//适用package

6. 如果想要在适用者制作javaDoc文件的同时,也一并将Annotation的讯息加入到Api文件中,使用java.lang.annotation.Docuented

例如:

@Documented//该注解表示将DocumentedAnnotation在适用类中的注解信息加入到api文档中
public @interface DocumentedAnnotation {

	String hello();
}

测试类

public class DocumentedTest {

	@DocumentedAnnotation(hello="welcome")
	public void method(){
		System.out.println("hello world");
	}
}

则生成的DocumentedTest类的api文件中,method()方法的头上将出现注解
@DocumentedAnnotation(hello="welcome")
字样。

小技巧:

在myeclipse中生成注解的方法:projectàgenerate javadoc



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值