Java中内置诠释详解
一、 Java 注释( Annotation ) :
1 ) @Override 注释
该注释仅应用于方法,用来指明被其注释的方法必须重写超类中的方法,否则会发生编译错误。
2 ) @Deprecated 注释
该注释用于声明元素已经过时,不鼓励使用。如果坚持使用,可能会带来潜在问题。如下例子:
import java.util.ArrayList;
import java.util.List;
public class AnnoSamp1 {
@Deprecated
public void method1(){}
public List method2(){
List list = new ArrayList();
list.add("007");
return list;
}
}
注:此处程序代码见相册中,有实际运行情况!
方法
method1()
已经被注释为过时方法,所以如果由另外一个程序调用了
AnnoSamp1
()类的
method1()
方法,编译时会提示过时。
3 ) @SuppressWarnins 注释
SuppressWarnins 注释允许开发人员控制编译器警告的发布,例如泛型使所有的类型安全操作成为可能,如果没有使用泛型而存在类型安全问题,编译器将会抛出警告。 SuppressWarnins 使用格式为:
@SuppressWarnins( 参数名 )
参数表如下:
( 1 ) deprecated: 过时的类或方法
( 2 ) finally:finally 子句无法正常完成
( 3 ) fallthrough:switch 程序块中没有使用 break
( 4 ) serial: 类缺少 serialVersionUID
( 5 ) unchecked: 未经检查的类型转换
( 6 ) unused: 定义了但从未使用
( 7 ) all: 以上全部情况
4 ) @Target 注释
Target 注释用来限制注释的使用范围,其使用格式是:
@Target({ 应用类型 1 ,应用类型 2 , …….})
其中应用类型如下:
( 1 ) TYPE :类、接口、注释或枚举类型
( 2 ) FIELD :属性,包括枚举常量
( 3 ) METHOD :方法
( 4 ) PARAMETER :参数
( 5 ) CONSTRUCTOR :构造方法
( 6 ) LOCAL_VARIABLE :局部变量
( 7 ) ANNOTATION_TYPE :注释类
( 8 ) PACKAGE :包
二、自定义注释
可以为自己创建的注释类提供成员,例如:
Public @interface InTesting{
String value();
}
这是只有一个成员的注释(单成员注释),可以这样来使用它:
@InTesting(“This method returns all department info”)
Public void getMsg(){….}
由更简便的使用方法:
@InTesting(“This method returns all department info”)
Public void getMsg(){…}
当然,注释不只是单成员这么简单,可以更为复杂些,例如:
Public @interface InTesting{
String author();
String description() default “Testing”;
float ver();
}
三、保留策略
Java 提供了 3 种保留策略:
1 ) RetentionPolicy.CLASS: 编译后保留在 class 文件中,运行时忽略。
2 ) RetentionPolicy.RUNTIME: 编译后保留在 class 文件中,运行时 Java 虚拟机可读取。
3 ) RetentionPolicy.SOURCE: 只存在于源文件中,编译后保留在 class 文件中 .
例如:
import java.lang.annotion.Target;
import java.lang.annotion.ElementType.METHOD;
import java.lang.annotion.Retention;
import java.lang.annotion.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
@Target({METHOD})
public @interface InTesting{
String author();
String description() default "Testing";
float ver ();
}
四、读取注释
Java 的 java.lang.reflect 包中提供了 getAnnotations() 方法,以数组的形式返回注释信息。该方法可用于类、方法、构造方法等类型的对象中。示例如下:
首先来编写一个简单的类 Message ,该类的两个方法均被注释:
public class Message{
@InTesting(author = "cg ",description = " 获取备注信息 ",ver = 1.1f)
public void getMsg(){
System.out.println("get message info....");
}
@InTesting(author = "cg ",description = " 计算利润率,测试中 ",ver = 1.2f)
public void calcProfitRat(){
System.out.println("Calculate...");
}
}
接下来编写一个打印输入 Message 类所有方法注释信息的程序:
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
public class ReadAnno{
public static void main(String[] args ) throws SecurityException,NoSuchMethodException{
Class<Message> c = Message.class;// 获得 Message 类的实例
Method[] m = c.getMethods(); // 获取该类的全部方法
Annotation[] annotations;// 声明注释变量
for(Method method:m){
annotations = method.getAnnotations(); // 获得当前方法的注释信息
System.out.println(annotation);
}
}
}
程序运行后输出结果如下:
@ cn.edu.sinomacro.coursemanag .InTesting(description= 获取备注信息 ,author=cg ,ver =1.1)
@cn.edu.sinomacro.coursemanag.InTesting(description= 计算利润率,测试中 ,author=cg ,ver =1.2)
注意: cn.edu.sinomacro.coursemanag 是自定义的包名。