内置注解
public class Test01 extends Object {
@Override
public String toString() {
return super.toString();
}
@Deprecated
public static void test01(){
System.out.println("Deprecated");
}
public static void main(String[] args) {
test01();
}
}
运行结果:
Deprecated
元注解
import java.lang.annotation.*;
public class Test02 {
public void test(){
}
}
@Target(value = {ElementType.METHOD,ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
@Documented
@Inherited
@interface MyAnnotation{
}
自定义注解
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
public class Test03 {
@MyAnnotation2(name = "阿涛",school = "轻工大")
public void test() {
}
@MyAnnotation3("涛")
public void test2(){
}
}
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2{
String name() default "";
int age() default 0;
int id() default -1;
String[] school();
}
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation3{
String value();
}