1.4 自定义注解

package annotation;
import javax.xml.bind.Element;
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="xiaoming",age = 10,id=1,schools = "qinghua")
public void test(){}
}
@Target({ElementType.TYPE,ElementType.METHOD})
@Rentention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2{
//注解的参数 : 参数类型 + 参数名 ();
String name() default "";
int age() default 0;
int id() default -1;//如果默认值为-1,代表不存在,indexof,如果找不到就返回-1
String[] schools() default {"清华","北大"};
}
//注解
//使用default定义默认值
//注解可以显示赋值,如果没有默认值,我们就必须给显示给注解赋值
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation3{
String value();
//注解中只有一个参数的时候,建议使用value作为参数的名字
}