注解:
-
AnnotationJDK5.0开始引入的;
-
Annotation的作用:
可以被其他程序(比如:编译器)读取;
可以对程序做出解释;
-
内置注解的例子:
@Override:重写方法的注解;
@Deprecated:过时的方法的注解,不推荐使用,但是可以使用;
@SuppressWarinnings(“all”):可以放在类上面,方法上面,警告的注解,加上它,可以取消警告;
-
元注解的例子
@Target:作用域;
@Documents:运行时级别
扫描二维码关注公众号,回复: 6579179 查看本文章@Retention:生成doc文档时使用
@Inherited:自雷可以继承父类的注解
-
自定义注解:
@MyAnnotation(value = "hyf",age = 1,id = 2,schools = {"a"}) @SuppressWarnings("all") public class Test1 { @MyAnnotation @MyAnnotation3("123") public static void main(String[] args) { } } //作用域 @Target(value = {ElementType.METHOD, ElementType.TYPE, ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @interface MyAnnotation { String value() default ""; int age() default 0; int id() default -1; //-1代表不存在 String[] schools ()default {""}; } @interface MyAnnotation3{ String value(); }