读书人

Annotation 之 jdk1.5内建的Annotatio

发布时间: 2012-09-27 11:11:17 作者: rapoo

Annotation 之 jdk1.5内建的Annotation实例

一、限定Override父类方法@Override

java.lang.Override是个Marker annotation
用于标示的Annotation,Annotation名称本身即表示了要给工具程序的信息

实例:

package com.bhan.annotation;

public class?OverrideTest {

?@Override
?public String toString() {
??return "this is override";
?}
?
?public static void main(String[] args) {
??OverrideTest test = new OverrideTest();
??System.out.println(test.toString());
?}
}

二、标示方法为Deprecated @Deprectated

对编译程序说明某个方法已经不建议使用,即该方法是过时的。
java.lang.Deprecated也是Marker annotation
Deprecated这个名称在告知编译程序,被@Deprecated标示的方法是一个不建议被使用的方法

实例:

package com.bhan.annotation;

public class DeprecatedTest {

?@Deprecated
?public void doSomthing(){
??System.out.println("do something");
?}
?
?public static void main(String[] args) {
??DeprecatedTest test = new DeprecatedTest();
??test.doSomthing();
?}
}
三、抑制编译程序警告@SuppressWarnings

对编译程序说明某个方法中若有警告讯息,则加以抑制

实例:

package com.bhan.annotation;

import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

public class SuppressWarningTest {

?@SuppressWarnings(value={"unchecked","deprecation"})
?public static void main(String[] args) {
??//Map<String,Date> map = new TreeMap<String, Date>();
??Map map = new TreeMap();
??map.put("hello",new Date());
??
??System.out.println(map.get("hello"));
??
??DeprecatedTest test = new DeprecatedTest();
??test.doSomthing();
?}
}

读书人网 >编程

热点推荐