天天看点

java基础:注解

四种元注解:作用在其他注解上

@Target:表示我们的注解可以用在什么地方

@Retention:表示我们的注解在什么地方有效 作用范围:runtime>class>sources

@Documented:表示是否将我们的注解生成javaDoc中

@Inherited:表示子类可以继承父类的注解

自定义注解:

`@Target(value = {ElementType.TYPE,ElementType.METHOD})

@Retention(value= RetentionPolicy.CLASS)

@interface myAnnotation1{

//注解的参数: 参数类型+参数名();

String name() default "";

int age() default 0;

String[] students() default {"zsuz","hefei"};

}<code>自定义注解(当参数只有一个的时候可以使用:value()):</code>@interface myAnnotation2{

//如果只有一个参数 调用时value可以省略

String value();

}`