Java中的注解

1.了解注解

注解很重要,未来的开发模式都是基于注解,注解是一种趋势,现在已经有不少人开始使用注解了,注解是JDK1.5之后才有的新特性。包含在java.lang.annotation包中,它是附加在代码中的一些元信息,将一个类的外部信息和内部成员联系起来,在编译、运行时进行解析和使用。Java内置了一些注解,还支持自定义注解,一些知名的框架都有自己实现的自定义注解,也可以自己定义注解供使用。

注解的一般格式是[修饰符] @interface [名称] {元素},元素是无方法体的方法声明,可以有默认值。

2.元注解

@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Description {
    String desc();
    String author();
    int age() default 18;
}

元注解的作用是负责注解其他注解,注解的注解。Java5.0定义了4个标准的meta-annotation类型,它们用来对其他annotation类型作说明。Java5.0定义的元注解:

  1. @Target
  2. @Retention
  3. @Documented
  4. @Inherited

@Target:

说明了注解所修饰的对象范围。取值是来源于Java.lang.annotation。ElementType的枚举类型元素:

	1.CONSTRUCTOR:用于描述构造器
	2.FIELD:用来描述域
	3.LOCAL_VARIABLE:用于描述局部变量
	4.METHOD:用于描述方法
	5.PACKAGE:用于描述包
	6.PARAMETER:用于描述参数
	7.TYPE:用于描述类、接口(包括注解类型)或enum声明

@Retention:

@Retention定义了该Annotation被保留的时间长短。取值来java.lang.annotation.RetentionPolicy的枚举类型值:

1.SOURCE:在源文件中有效(即源文件保留)
2.CLASS:在class文件中有效(即class保留)
3.RUNTIME:在运行时有效(即运行时保留)

@Documented:
@Documented用于描述其他类型的annotation应该被作为标注的程序成员的公共API,因此可以被例如javadoc此类的工具文档化。Documented是一个标记注解,没有成员。

@Inherited:
@Inherited元注解是一个标记注解,@Inherited阐述了某个被标注的类型是被继承的。如果一个使用了@Inherited修饰的annotation类型被用于一个class,则这个annotation将被用于该class的子类。

3.JDK的注解

1.JDK注解

JDK注解一共分三类。
在这里插入图片描述

  1. @Override:告知编译器此方法是覆盖父类的
  2. @Deprecated:标注过时@Deprecated:标注过时
  3. @SuppressWarnings:压制警告

4.注解实现的实例

注解通过反射获取。首先可以通过 Class 对象(实现了AnnotatedElement接口)的 isAnnotationPresent() 方法判断它是否应用了某个注解

 public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {}

然后通过 getAnnotation() 方法来获取 Annotation 对象。

public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {}

或者是 getAnnotations() 方法。

public Annotation[] getAnnotations() {}

前一种方法返回指定类型的注解,后一种方法返回注解到这个元素上的所有注解。

如果获取到的 Annotation 如果不为 null,则就可以调用它们的属性方法了。

实现注解需要的三个条件:注解声明、使用注解的元素、操作注解使其起作用
1.定义一个MyTag的注解类:

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD,ElementType.FIELD})
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface MyTag {
	String name() default "车";
	int size() default 10;
}

实体类:

public class Car {
	private String name;
	private int size;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getSize() {
		return size;
	}
	public void setSize(int size) {
		this.size = size;
	}
	public Car(String name,int size){
		this.name = name;
		this.size = size;
	}
}

2.使用注解的类

public class AnnotationDemo {
	@MyTag(name="audi",size=10)
	private Car car;
	public Car getCar() {
		return car;
	}
	public void setCar(Car car) {
		this.car = car;
	}
}

3.注解处理类
向注解处理类中传入要是使用注解的类:

public class AnnotationProccessor {
	public static void annoProcess(AnnotationDemo annotation){
		for(Field field:annotation.getClass().getDeclaredFields()){
			//使用反射得到域上面的注解
			MyTag myTag = field.getAnnotation(MyTag.class);
			//处理
			annotation.setCar(new Car(myTag.name(),myTag.size()));
		}
	}
}

4.测试类

public class TestDemo {
	public static void main(String[] args) {
		AnnotationDemo anDemo = new AnnotationDemo();
		AnnotationProccessor annoProcess = new AnnotationProccessor();
		annoProcess.annoProcess(anDemo);
		System.out.println(anDemo.getCar().getName());
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值