注解
什么是注解:
Annotation
是从JDK5.0开始引入的新技术
Annotation
的作用 :
- 不是程序本身 , 可以对程序作出解释.(这一点和注释(
comment)
没什么区别) - 可以被其他程序(比如:编译器等)读取
Annotation
的格式 :
注解是以"@注释名
"在代码中存在的 , 还可以添加一些参数值 , 例如:@SuppressWarnings(value="unchecked")
Annotation
在哪里使用?
可以附加在package
, class
, method
, field
等上面 , 相当于给他们添加了额外的辅助信息, 我们可以通过反射机制编程实现对这些元数据的访问
在之前的代码编写中,经常会用到重写,在我们重写的方法上会有一个特殊标记@Override
,这个标记代表重写,这个标记叫做注解,和我们之前用到的注释不同,注释只是给程序员和用户观看,方便理解代码的逻辑,对计算机本身而言并没有什么用处,而注解不但可以给程序员起到提示的作用,也会给计算机起到提示的作用
查看Override
源码:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
上面的两个注解为元注解,元注解是注解的注解,一共有四个元注解:
-
@Target(value ={ElementType.METHOD,ElementType.TYPE,ElementType.FIELD})
上面的注解中的参数决定注解的作用域
@Retention(RetentionPolicy.RUNTIME)
参数决定注解运行时级别@Documented
产生DOC文档时候使用@Inherited
子类可以继承父类的注解
元注解源码查看
Target:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE) //前三个都是元注解
public @interface Target {
/**
* Returns an array of the kinds of elements an annotation type
* can be applied to.
* @return an array of the kinds of elements an annotation type
* can be applied to
*/
ElementType[] value(); //参数是ElementType类的数组
//参数类型 //参数名字
}
查看 ElementType[]
public enum ElementType { //枚举类型
/** Class, interface (including annotation type), or enum declaration */
TYPE, //表示作用域是类
/** Field declaration (includes enum constants) */
FIELD, //成员变量
/** Method declaration */
METHOD, //方法 //注释的这三个也是最常用的
/** Formal parameter declaration */
PARAMETER,
/** Constructor declaration */
CONSTRUCTOR,
/** Local variable declaration */
LOCAL_VARIABLE,
/** Annotation type declaration */
ANNOTATION_TYPE,
/** Package declaration */
PACKAGE,
/**
* Type parameter declaration
*
* @since 1.8
*/
TYPE_PARAMETER,
/**
* Use of a type
*
* @since 1.8
*/
TYPE_USE
}
要注意,使用该元注解时如果有多个参数,value
不能省略,参数使用大括号括起,中间使用逗号隔开
Retention:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
/**
* Returns the retention policy.
* @return the retention policy
*/
RetentionPolicy value();
}
查看RetentionPolicy
:
public enum RetentionPolicy {
/**
* Annotations are to be discarded by the compiler.
*/
SOURCE,
/**
* Annotations are to be recorded in the class file by the compiler
* but need not be retained by the VM at run time. This is the default
* behavior.
*/
CLASS,
/**
* Annotations are to be recorded in the class file by the compiler and
* retained by the VM at run time, so they may be read reflectively.
*
* @see java.lang.reflect.AnnotatedElement
*/
RUNTIME //最常用
}
Documented:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {
}
该元注解无参数,直接使用
Inherited:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Inherited {
}
内置注解
@Override
: 定义在 java.lang.Override 中 , 此注释只适用于修辞方法 , 表示一个方法声明打算重写超类中的另一个方法声明.
@Deprecated
: 定义在java.lang.Deprecated中 , 此注释可以用于修辞方法 , 属性 , 类 , 表示不鼓励程序员使用这样的元素 , 通常是因为它很危险或者存在更好的选择
@SuppressWarnings
: 定义在java.lang.SuppressWarnings中,用来抑制编译时的警告信息. 与前两个注释有所不同,你需要添加一个参数才能正确使用,这些参数都是已经定义好了的, 我们选择性的使用就好了
@SuppressWarnings("all")
@SuppressWarnings("unchecked")
@SuppressWarnings(value={"unchecked","deprecation"})
- 等等…
自定义注解
使用 @interface
自定义注解时 , 自动继承了java.lang.annotation.Annotation接口
分析 :
@ interface
用来声明一个注解 , 格式 :public @ interface 注解名 { 方法 }
- 其中的每一个方法实际上是声明了一个配置参数
- 方法的名称就是参数的名称.
- 返回值类型就是参数的类型 ( 返回值只能是基本类型,
Class
,String
,enum
) - 可以通过default来声明参数的默认值
- 如果只有一个参数成员 , 一般参数名为
value
- 注解元素必须要有值,我们定义注解元素时,经常使用空字符串,
0
作为默认值.
自定义注解示例:
@Target(value = {ElementType.METHOD,ElementType.TYPE,ElementType.FIELD}) // 作用域
@Retention(RetentionPolicy.RUNTIME) //运行时级别
@Documented // 生产DOc文档时候使用
@Inherited //子类可以继承父类的注解
@interface MyAnnotation{
}
通过上述格式便可完成自定义注解,之后可以直接使用
查看Thread
类:
在这里可以看到个别方法上有横线 ,说明该方法已经废弃或者有更好的方法来代替,其实使用的是@Deprecated
注解
代码示例:
在给stop
方法加上@Deprecated
注解后,可以看到stop
方法在被调用时在上面自动加上了删除线,来提示用户改方法已经过时或者有更好的方法来替代
将注解注释掉可以看到删除线消失,这就是注解的作用,可以起到提示计算机和用户的功能,功能比较强大,在以后还会进行及时的比较详细的补充