首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Annotation 之 jdk1.5内建的Annotation范例

2012-09-27 
Annotation 之 jdk1.5内建的Annotation实例一、限定Override父类方法@Overridejava.lang.Override是个Marke

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();
?}
}

热点排行