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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值