注解(Annotation)
目录
- 注解(Annotation)
- 1. 注解入门
- 1. 作用
- 2. 格式
- 2. 内置注解
- 1. @Override
- 2. @Deprecated
- 3. @SuppressWarnings
- 3. 元注解
- 1. 作用
- 2. 4个标准的meta-annotation类型
- 4. 自定义注解
- 1. 注解入门
1. 注解入门
1. 作用
- 不是程序本身,可以对程序作出解释(这一点和注释(comment)没什么区别)
- 可以被其他程序(比如编译器)读取
2. 格式
注解是以"@注释名"在代码中存在的 ,可以添加一些参数值,如@SuppressWarnings(value="unchecked")
2. 内置注解
1. @Override
此注释只适用于修饰方法,表示一个方法声明打算重写超类中的另一个方法申明
2. @Deprecated
此注释可应用于修饰方法,属性,类,表示不鼓励程序员使用这样的元素,通常是因为它很危险或者存在更好的选择
3. @SuppressWarnings
用来抑制编译时的警告信息,需要添加一个参数才能正确使用,这些参数已经定义好了
- @SuppressWarnings("all")
- @SuppressWarnings("unchecked")
- @SuppressWarnings(value = {"uncheckedl", "deprecation"})
- ......
package com.wang.annotation;
import java.util.ArrayList;
import java.util.List;
//什么是注解
public class Test01 extends Object {
//@override 重写的注解
@Override
public String toString() {
return super.toString();
}
//Deprecated 不推荐程序员使用,但是可以使用,或存在更好的方式
@Deprecated
public static void test() {
System.out.println("Deprecated");
}
@SuppressWarnings("all")
public static void test02() {
List list = new ArrayList();
}
public static void main(String[] args) {
test();
}
}
3. 元注解
1. 作用
元注解的作用就是负责注解其他注解,JAVA定义了4个标准的meta-annotation类型,他们被用来提供对其他annotation类型作说明
2. 4个标准的meta-annotation类型
-
@Target:用于描述注解的使用范围(被描述的注解可以用在什么地方)
-
@Retention:表示需要在什么级别保存该注释信息,用于描述注解的生命周期
- (SOURCE< CLASS< RUNTIME)
-
@Documented:说明该注解将被包含在javadoc中
-
@Inherited:说明子类可以继承父类中的该注解
package com.wang.annotation;
import java.lang.annotation.*;
//测试元注解
@MyAnnotation
public class Test02 {
@MyAnnotation
public void test() {
}
}
//定义一个注解
//Target 表示我们的注解可以用在哪些地方
//METHOD:方法 TYPE:类
@Target(value = {ElementType.METHOD, ElementType.TYPE})
//Retention 表示我们的注解在什么地方还有效
//RUNTIME > CLASS > SOURCE
@Retention(value = RetentionPolicy.RUNTIME)
//Documented 表示我们的注解生成在Javadoc中
@Documented
//Inherited 子类可以继承父类的注解
@Inherited
@interface MyAnnotation {
}
4. 自定义注解
使用@interface自定义注解,自动继承了java.lang.annotation.Annotation接口
分析:
- @interface用来声明一个注解,格式:[public] @interface 注解名 {定义内容} 其中,[ ]为可选属性
- 其中的每一个方法实际上是声明了一个配置参数
- 方法的名称就是参数的名称
- 返回值类型就是参数的类型(返回值只能是基本类型:Class,String,enum)
- 可以通过default来声明参数的默认值
- 如果只有一个参数成员,一般参数名为value
- 注解元素必须要有值,我们定义注解元素时,经常使用空字符串以及0作为默认值
package com.wang.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//自定义注解
public class Test03 {
//注解可以显式赋值,如果没有默认值,我们就必须给注解赋值
@MyAnnotation2(name = "wang", age = 18, id = 1, schools = {"WPI","NEU"})
private void test() {}
//如果注解只有一个值,同时该值使用value命名,则此时注解上可以省略value =
@MyAnnotation3("wang")
public void test2() {}
}
@Target({ElementType.ANNOTATION_TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2{
//注解的参数:参数的类型 + 参数名();
String name() default "";
int age() default 0;
int id() default -1; //如果默认值为-1,代表不存在
String[] schools() default {"NYU","TAMU"};
}
@Target({ElementType.ANNOTATION_TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation3{
String value();
}