Annotation自定義註解
阿新 • • 發佈:2018-12-10
自定義註解示例:
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介面: