标签:
java注解的有关知识
package com.atguigu.java; import java.util.ArrayList; import java.util.List; /* * 注解 * 1.JDK提供的常用的注解 * @Override:限定重写父类方法,该注释只能用于方法 * @Deprecated:用于表示某个程序元素(类,方法等)已过时 * @SupressWarnings:抑制编译器警告 * 2.如何自定义一个注释 * 3.元注解 * */ public class TestAnnotation { public static void main(String[] args){ Person person=new Student("tom",21); person.walk(); person.eat(); @SuppressWarnings({ "unused", "rawtypes" }) List list=new ArrayList(); @SuppressWarnings("unused") int i=10; } } class Student extends Person{ public Student(String name, int age) { super(name, age); } @Override public void walk(){ System.out.println("学生走路"); } @Override public void eat(){ System.out.println("学生吃饭"); } } @Deprecated class Person{ private String name; private int age; public Person(String name, int age) { super(); this.name = name; this.age = age; } public void walk(){ System.out.println("走路"); } @Deprecated public void eat(){ System.out.println("吃饭"); } @Override public String toString(){ return "Person [name="+name+",age="+age+"]"; } }
package com.atguigu.java; //自定义的注解 public @interface MyAnnotation { String value() default "hello"; } package com.atguigu.java; import java.util.ArrayList; import java.util.List; /* * 注解 * 1.JDK提供的常用的注解 * @Override:限定重写父类方法,该注释只能用于方法 * @Deprecated:用于表示某个程序元素(类,方法等)已过时 * @SupressWarnings:抑制编译器警告 * 2.如何自定义一个注释 * 3.元注解 * */ public class TestAnnotation { public static void main(String[] args){ Person person=new Student("tom",21); person.walk(); person.eat(); @SuppressWarnings({ "unused", "rawtypes" }) List list=new ArrayList(); @SuppressWarnings("unused") int i=10; } } @MyAnnotation(value="atguigu") class Student extends Person{ public Student(String name, int age) { super(name, age); } @Override public void walk(){ System.out.println("学生走路"); } @Override public void eat(){ System.out.println("学生吃饭"); } } @Deprecated class Person{ private String name; private int age; @MyAnnotation(value="atguigu") public Person(String name, int age) { super(); this.name = name; this.age = age; } @MyAnnotation(value="atguigu") public void walk(){ System.out.println("走路"); } @Deprecated public void eat(){ System.out.println("吃饭"); } @Override public String toString(){ return "Person [name="+name+",age="+age+"]"; } }
package com.atguigu.java; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import static java.lang.annotation.ElementType.TYPE; import static java.lang.annotation.ElementType.FIELD; import static java.lang.annotation.ElementType.CONSTRUCTOR; import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.ElementType.PARAMETER; import static java.lang.annotation.ElementType.LOCAL_VARIABLE; //自定义的注解 @Target({TYPE,FIELD,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE}) @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { String value() default "hello"; }
标签:
原文地址:http://www.cnblogs.com/xujiming/p/4637545.html