注解
JDK 5.0 Annotation
注解可以被其他程序读取
内置注解
@Deprecated //不推荐使用 已经被淘汰 或者又更好的方法 @Override //重写方法 @SuppressWarnings() //镇压警告 有参数
元注解
复制解释其他注解的注解 meta-annotation
@Target @Retention @Documented @Inherited
import java.lang.annotation.*; @MyAnnotation public class Test01 { @MyAnnotation public void test(){ } } //注解可以表示 我们的注解可以用在什么地方 @Target(value = {ElementType.METHOD,ElementType.TYPE}) @Retention(value = RetentionPolicy.RUNTIME) //什么时候有效 Runtime>Class>Source @Documented //表示是否我们在注解的生成在JavaDoc中 @Inherited //子类可以继承父类的注解 @interface MyAnnotation{ //@interface 来自定义一个注解 }
自定义注解
public class Test03 { @MyAnnotation2() void test(){ } } @Target({ElementType.TYPE,ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @interface MyAnnotation2{ //注解参数: 类型加 + 名字(); String name() default ""; int age() default 0; int id() default -1; //默认值-1 表示不存在 String[] schools() default {"A","B"}; }