自定义注解示例:
import java.lang.annotation.*;
@Target(value = {ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface MyFactory {
String name() default "aa";
String val();
}
@MyFactory(val = "12")
public class student implements Serializable {
}
import java.lang.annotation.Annotation;
public class Test {
public static void main(String[] args) {
Class<student> c=student.class;
Annotation a=c.getAnnotation(MyFactory.class);
System.out.println(a);
System.out.println(((MyFactory) a).name());
System.out.println(((MyFactory) a).val());
}
}
运行结果:
@other.annotation.MyFactory(name=aa, val=12)
aa
12
使用@interface标注的MyFactory自动继承Annotation接口: