Java----注解

高新技术 注解
一、注解
注解:是一个特殊的类,JDK1.5版本新特性。注解相当于一种标记,在程序中加了注解就等于为程序打上了某种标记。
eclipse下的编译特点

提示该方法已经过时了。

cmd下的编译特点

javac -Xlint:deprecation编译查看详情

解决办法:为了消除这些提示,可以为此方法加上注解。
package cn.itcast.day2;

public class AnnotationTest {

	@SuppressWarnings("deprecation")
	public static void main(String[] args) {
		System.runFinalizersOnExit(true);
	}
}

这样,提示就消失了。
注解应用:提示新手不要用,告诉老手已过时。
package cn.itcast.day2;

public class AnnotationTest {

	@SuppressWarnings("deprecation")
	public static void main(String[] args) {
		System.runFinalizersOnExit(true);
		show();
	}

	@Deprecated	//将show方法标识为过时的方法,作用:提示新手不要用,告诉老手已过时。
	private static void show() {
		// TODO Auto-generated method stub
		System.out.println("Hello World");
	}
	
}

标记方法为覆盖,如果没有覆盖,则在编译时期,就会出错。把运行时期出现的错误转移到了编译时期,避免了不必要的错误。

以后,在javac编译器,开发工具和其它程序可以用反射来了解你的类及各种元素上是否有无何种标记,看你有什么标记,就去干相应的事,标记可以加在字段方法方法的参数以及局部变量上。
注解的应用结构图

注解类
package cn.itcast.day1;

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

public @interface MyAnnotation {

}
应用注解的类:
package cn.itcast.day1;

@MyAnnotation
public class AnnotionTest {

	public static void main(String[] args) {
		if(AnnotionTest.class.isAnnotationPresent(MyAnnotation.class)){//检查是否有这个注解
			MyAnnotation ma = AnnotionTest.class.getAnnotation(MyAnnotation.class);//获得这个注解对象
			System.out.println(ma);
		}
	}

}
运行后没有显示任何信息。疑问这是为什么呢?
这说明if语句的值是false,检查处没有这个注释,因为注释三个生命周期
1.SOURCE(只保留在源码里)。
2.CLASS(只保留在.class文件里)。
3.RUNTIME(可以保留到运行时期)。
定义注解时需要定义生命周期@Retention(RetentionPolicy.生命周期),默认为CLASS
也可以添加多个注释,如:定义该注释的作用范围@Target(ElementType.METHOD)
1.ANNOTATION_TYPE注释类型声明。
2.CONSTRUCTOR构造方法声明。
3.FIELD字段声明(包括枚举常量)。
4.LOCAL_VARIABLE局部变量声明。
5.METHOD方法声明。
6.PACKAGE包声明。
7.PARAMETER参数声明。
8.TYPE类、接口(包括注释类型)或枚举声明。
:这些定义在注解中的注解称为元注解
改进
package cn.itcast.day1;

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

@Retention(RetentionPolicy.RUNTIME)//指定标记保持到运行时期
@Target({ElementType.METHOD,ElementType.TYPE})//指定注解必须定义到方法和类上
public @interface MyAnnotation {

}

注解属性:为了更明确的区分和扩展,可以为注解添加属性。效果@MyAnnotation(collor = "RED")
package cn.itcast.day1;

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

@Retention(RetentionPolicy.RUNTIME)//指定标记保持到运行时期
@Target({ElementType.METHOD,ElementType.TYPE})//指定注解必须定义到方法和类上
public @interface MyAnnotation {
	String color();
}
其实这个属性是一个抽象方法。接着要为这个属性添加属性值
package cn.itcast.day1;

@MyAnnotation(color="RED")//为属性添加属性值。
public class AnnotionTest {

	public static void main(String[] args) {
		if(AnnotionTest.class.isAnnotationPresent(MyAnnotation.class)){//检查是否有这个注解
			MyAnnotation ma = AnnotionTest.class.getAnnotation(MyAnnotation.class);//获得这个注解对象
			System.out.println(ma.color());
		}
	}

}

:如果属性名为“value”,可以省略“value=”。
package cn.itcast.day1;

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

@Retention(RetentionPolicy.RUNTIME)//指定标记保持到运行时期
@Target({ElementType.METHOD,ElementType.TYPE})//指定注解必须定义到方法和类上
public @interface MyAnnotation {
	String value();//添加字符串类型属性
}
package cn.itcast.day1;

@MyAnnotation("RED")
public class AnnotionTest {

	public static void main(String[] args) {
		if(AnnotionTest.class.isAnnotationPresent(MyAnnotation.class)){//检查是否有这个注解
			MyAnnotation ma = AnnotionTest.class.getAnnotation(MyAnnotation.class);//获得这个注解对象
			System.out.println(ma.value());
		}
	}

}
注解指定缺省属性:在注解类的属性后面加“default 相应类型的属性”。如:String color() default "RED";
package cn.itcast.day1;

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

@Retention(RetentionPolicy.RUNTIME)//指定标记保持到运行时期
@Target({ElementType.METHOD,ElementType.TYPE})//指定注解必须定义到方法和类上
public @interface MyAnnotation {
	String color() default "RED";//添加缺省值
	String value();
}
package cn.itcast.day1;

@MyAnnotation("ABC")
public class AnnotionTest {

	public static void main(String[] args) {
		if(AnnotionTest.class.isAnnotationPresent(MyAnnotation.class)){//检查是否有这个注解
			MyAnnotation ma = AnnotionTest.class.getAnnotation(MyAnnotation.class);//获得这个注解对象
			System.out.println(ma.value()+"::"+ma.color());
		}
	}

}

添加数组属性
例:
package cn.itcast.day1;

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

@Retention(RetentionPolicy.RUNTIME)//指定标记保持到运行时期
@Target({ElementType.METHOD,ElementType.TYPE})//指定注解必须定义到方法和类上
public @interface MyAnnotation {
	String color() default "RED";
	String value();
	int[] intarr() default {1,2,3,4};//添加数组类型属性
}
:如果数组中只有一个元素,那么可以省略"{ }"。
注解添加注解属性:
package cn.itcast.day1;

public @interface OtherAnnotation {
	String value();
}
package cn.itcast.day1;

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

@Retention(RetentionPolicy.RUNTIME)//指定标记保持到运行时期
@Target({ElementType.METHOD,ElementType.TYPE})//指定注解必须定义到方法和类上
public @interface MyAnnotation {
	String color() default "RED";
	String value();
	int[] intarr() default {1,2,3,4};
	OtherAnnotation oat() default @OtherAnnotation("123");//添加注解类型属性
}
package cn.itcast.day1;

@MyAnnotation("ABC")
public class AnnotionTest {

	@MyAnnotation(value="abc",intarr={3,4,5},oat=@OtherAnnotation("abc"))
	public static void main(String[] args) {
		if(AnnotionTest.class.isAnnotationPresent(MyAnnotation.class)){//检查是否有这个注解
			MyAnnotation ma = AnnotionTest.class.getAnnotation(MyAnnotation.class);//获得这个注解对象
			System.out.println(ma.value()+"::"+ma.color()+"::"+ma.intarr().length+"::"+ma.oat().value());
		}
	}

}

添加Class类型属性:
package cn.itcast.day1;

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

@Retention(RetentionPolicy.RUNTIME)//指定标记保持到运行时期
@Target({ElementType.METHOD,ElementType.TYPE})//指定注解必须定义到方法和类上
public @interface MyAnnotation {
	String color() default "RED";
	String value();
	int[] intarr() default {1,2,3,4};
	OtherAnnotation oat() default @OtherAnnotation("123");
	Class clazz() default MyAnnotation.class;//添加Class类型属性
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值