注解:
1: jdk5提供了3个标准注解,也叫标注注解,不影响程序运行
* @Override // 标记这是重写的父类的方法
* @SuppressWarnings("unused") //压制 消除警告
* @Deprecated //标注此方法已过时
2:自定义注解 通过关键词@interface修饰
创建:
- public @interface MyAnnotation {
-
- String name();
- int age();
- Class shuxing();
- Color color();
- String[] hobby();
- String value() default "nihao";
- }
- enum Color {
- REG,
- GREEN,
- BLUE
- }
使用方式:
- @MyAnnotation(name = "zhangsan", age = 1, shuxing = Student.class, value = "lisi")
- @MyAnnotation("zhangsan")
- @MyAnnotation(hobby={"football", "basketball"})
- @MyAnnotation(color=Color.REG)
3:jdk5提供的4个元注解,修饰注解的注解
元注解可以修饰自己定义的注解
- @Retention(RetentionPolicy.SOURCE)
- @Retention(RetentionPolicy.CLASS)
- @Retention(RetentionPolicy.RUNTIME)
- @Target({ElementType.METHOD, ElementType.FIELD, ElementType.TYPE})
- @Documented
- @Inherited
使用元注解,修饰了自定义注解只能在方法上使用
- @Target(ElementType.METHOD)
- public @interface MyAnnotation {
- String value();
- }