@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