注解annotation

@Deprecated

表示过期的方法

@Override

表示重写方法

@SuppressWarnings("rawtypes")

忽略类型错误带来的警告

@SuppressWarnings({"deprecation","unchecked"})

//忽略编译器警告

public class Demo01 {

@Deprecated

public void cc()

{

System.out.println("ok");

}

//过期的方法

@Override

public String toString()

{

return "hello annotation";

}

public static void main(String[] args) {

Demo01 d1=new Demo01();

System.out.println(d1);

d1.cc();

JFrame jf=new JFrame("hello");

jf.setSize(300,300);

jf.setLocation(200,200);

jf.show();

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

@SuppressWarnings("rawtypes")

List list=new ArrayList();

list.add(10);

list.add("hi");

Iterator it= list.iterator();

while(it.hasNext())

{

System.out.println(it.next());

}

}

}

 

@Test

表示的是测试类

public @interface Test {

public String value();

public String name()default"abc";

}

 

public class Demo02 {

//如果annotation中有属性value 给值时可以忽略 value

@Test(value="lisi",name="sss")

public void cc()

{

System.out.println("ok");

}

@Test("wangwu")

public void dd()

{

System.out.println("haha");

}

public static void main(String[] args) {

Demo02 dm=new Demo02();

dm.cc();

dm.dd();

}

}

 

@Retention(RetentionPolicy.RUNTIME)

@Target(value={ElementType.TYPE,ElementType.METHOD,ElementType.FIELD})

@Inherited

@Documented

public @interface Test {

public String value();

public int age()default 22;

}

 

public class Demo01 {

//默认aa是给的名字

@Test("aa")

@Resource

public void cc()

{

System.out.println("ok");

}

public static void main(String[] args)throws Exception {

 

Class clazz=Demo01.class;

Method m=clazz.getDeclaredMethod("cc", new Class[]{});

Annotation ano[]=m.getDeclaredAnnotations();

for(Annotation a:ano)

{

System.out.println(a);

}

}

}

 

//让注释在运行的时候就显示

@Retention(RetentionPolicy.RUNTIME)

//注释的可以放的位置

@Target(value={ElementType.TYPE,ElementType.METHOD,ElementType.FIELD})

//可被继承

@Inherited

//文档中有显示

@Documented

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值