注解
注解复习
1、注解本质
注解本质就是一个借口 继承 java.lang.annotation.Annotation
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}
2、自定义注解
public @interface MyAnno {
}
3、使用注解
public class Main {
@MyAnno
void annotion() {
}
// TODO: 2021/6/30
@Override
public String toString() {
return super.toString();
}
}
4、常用注解
@Override: 重写父类方法
@Desperated : 废弃不建议使用
@SuperWarinning:忽视warning,警告。
5、注解属性
可以的返回值/类型
- 基本类型
- String
- 数组
- 枚举
注解里面的方法
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
ElementType[] value();
}
对注解赋值。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
}
// 需要传入一个数组ElementType[] value();
@Target(value = {ElementType.TYPE,ElementType.METHOD})
public @interface MyAnno3 {
}
6、元注解
@Target:描述注解能够作用的位置。
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
ElementType[] value();
}
@Retention:描述注解被保留的阶段。
@Retention(RetentionPolicy.RUNTIME)。 被描述的注解会被保留到class字节码文件中,并被jvm读取
@Documented:是否能被抽取到doc文档(文档中被看见)
@Inherited:是否被子类继承

浙公网安备 33010602011771号