一、什么是注解
了解什么是注解? Annotation
Annotation是从==JDK5.0== 开始引入的技术
-
注解的作用:
- 可以对程序做出解释 (类似于平时写的注释)
- ==可以被其他程序读取(如:编译器等)==
-
注解的格式:
-
以 @[注释名] 的形式在代码中存在,并且还可以添加一些参数
e.g. @SuppressWarnings(value = “unchecked”)
-
-
注解 可以用在哪?
- 附加在 ==package、class 、method、field==等上面,相当于给他们添加了额外的辅助信息
- 可以通过反射机制编程实现对元数据的访问
1. `package annotation;`
3. `// 什么是注解`
4. `public class Demo01 extends Object{`
5. `public static void main(String[] args) {`
7. `}`
8. `// 重写的注解`
9. `@Override`
10. `public String toString() {`
11. `return super.toString();`
12. `}`
13. `}`
内置注解
1、 @Override : 定义在java.lang. 包下,此注释只适用于修饰方法,表示一个方法声明打算重写超类/父类中的另一个方法声明
2、@Deprecated : 定义在java.lang.Deprecated中,可以用于修饰方法、属性、类,表示不鼓励程序员使用这样的元素,通常是因为它很危险(过时)或者存在更好地选择。
3、@SuppressWarnings : 定义在java.lang.SuppressWrnings中, 也叫正压警告,用来抑制编译时的警告信息。
- 与前两个注释有所不同,这里需要添加一个参数才能正确使用
- @SuppressWarnings(“all”)
- @SuppressWarnings(“unchecked”)
- @SuppressWarnings(value={“unchecked”, “deprecation”})
- …..
1. `package annotation;`
3. `import java.util.ArrayList;`
4. `import java.util.List;`
6. `public class Demo02 {`
7. `public static void main(String[] args) {`
8. `test();`
9. `}`
10. `@Deprecated // 不推荐程序员使用`
11. `public static void test() {`
12. `System.out.println("Deprecated!");`
13. `}`
15. `@SuppressWarnings("all")`
16. `public void test02() {`
18. `List list = new ArrayList<>();`
19. `}`
20. `}`
元注解 meta-annotation
-
元注解的作用
-
负责注解其他注解
Java定义了4个标准的meta-annotation类型,他们被用来提供对其他annotaiton类型作说明
-
-
这些类型和他们所支持的类放在 java.lang.annotation包中
- @Target:用于描述注解的使用范围(即:被描述的注解可以用在哪?)
- ==@Retention== : 表示需要在什么级别保存该注释信息,用于描述注解的生命周期
- Source < Class < Runtime
- @Documented : 说明该注解将被包含在 javadoc中
- @Inherited : 说明子类可以继承父类中的该注解
1. `package annotation;`
3. `import java.lang.annotation.*;`
5. `/*`
6. `元注解`
7. `*/`
9. `@MyAnnotation //not applitable to class`
10. `public class Demo03 {`
12. `@MyAnnotation`
13. `public void test() {`
15. `}`
17. `}`
19. `//定义一个注解类`
20. `// Target表示我们的注解可以用在哪些地方`
21. `@Target(value ={ElementType.METHOD,ElementType.TYPE} )`
22. `// Retention 表示注解在什么地方还有效`
23. `@Retention(value = RetentionPolicy.RUNTIME)`
24. `@Documented // 表示是否将为注解生成在 Javadoc中`
25. `@Inherited // 子类可以继承父类的注解`
26. `@interface MyAnnotation {`
28. `}`
自定义注解
1、使用 @interface 自定义注解时,自动继承了java.lang.annotation.Annotation接口
2、细节
-
@interface 用来声明一个注解
具体格式: public @interface 注解名 { }
其中的每一个方法实际上是声明了一个配置参数
方法的名称就是参数的名称
返回值类型就是参数的类型 (基本数据类型,Class,String,enum)
可以通过default来声明参数的默认值
如果只有一个参数成员,一般参数名为value
注解元素必须要有值 定义注解元素时,经常使用空字符串,0作为默认值
1. `package annotation;`
3. `// 自定义注解`
5. `import java.lang.annotation.ElementType;`
6. `import java.lang.annotation.Retention;`
7. `import java.lang.annotation.RetentionPolicy;`
8. `import java.lang.annotation.Target;`
10. `public class Demo04 {`
12. `// 注解可以显示赋值,若没有默认值,就必须给注解赋值,否则将报错`
13. `@MyAnnotation01(name = "lzh")`
14. `public void test() {`
16. `}`
18. `@MyAnnotation02("lzh")`
19. `public void test2() {`
21. `}`
22. `}`
24. `@Target(value = {ElementType.TYPE, ElementType.METHOD})`
25. `@Retention(RetentionPolicy.RUNTIME)`
26. `@interface MyAnnotation01 {`
28. `// 注解的参数 : 参数类型 + 参数名();`
29. `String name() default "";`
30. `int age() default 0;`
31. `int id() default -1; // 如果默认值为-1,代表不存在`
32. `String[] schools() default {"清华大学"};`
33. `}`
36. `@Target(value = {ElementType.TYPE, ElementType.METHOD})`
37. `@Retention(RetentionPolicy.RUNTIME)`
38. `@interface MyAnnotation02{`
40. `String value(); // 只有一个参数 默认为value`
41. `}`
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明,KuangStudy,以学为伴,一生相伴!