java注解
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({ </span>"unused", "rawtypes"<span style="color: rgba(0, 0, 0, 1)"> }) List list</span>=<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ArrayList(); @SuppressWarnings(</span>"unused"<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">int</span> i=10<span style="color: rgba(0, 0, 0, 1)">; }
}
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;</span><span style="color: rgba(0, 0, 255, 1)">public</span> Person(String name, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> age) { </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">(); </span><span style="color: rgba(0, 0, 255, 1)">this</span>.name =<span style="color: rgba(0, 0, 0, 1)"> name; </span><span style="color: rgba(0, 0, 255, 1)">this</span>.age =<span style="color: rgba(0, 0, 0, 1)"> age; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> walk(){ System.out.println(</span>"走路"<span style="color: rgba(0, 0, 0, 1)">); } @Deprecated </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> eat(){ System.out.println(</span>"吃饭"<span style="color: rgba(0, 0, 0, 1)">); } @Override </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String toString(){ </span><span style="color: rgba(0, 0, 255, 1)">return</span> "Person [name="+name+",age="+age+"]"<span style="color: rgba(0, 0, 0, 1)">; }
}
//自定义的注解 public @interface MyAnnotation {String value() default "hello"; }
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({ </span>"unused", "rawtypes"<span style="color: rgba(0, 0, 0, 1)"> }) List list</span>=<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ArrayList(); @SuppressWarnings(</span>"unused"<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">int</span> i=10<span style="color: rgba(0, 0, 0, 1)">; }
}
@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+"]";
}
}
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"; }