一。自定义注解(形体)
情形一: 单一注解
定义
public @interfaceMyAnnotation {//定义注解属性://属性类型 属性名() default 默认值;//String name() default "自定义名字";
String name();int age() default 20;//注解属性类型:基本类型,String,Class,注解,枚举,和 前面几种类型的一维数组//Student student; error:The blank final field student may not have been initialized
}
使用:
classDemo{//给注解赋值
@MyAnnotation(name="段哥哥",sex="男")public voidhello(){
System.out.println("hello");
}
}
情形二:注解的嵌套
定义
public @interfaceMyAnnotation2 {
String url()default "";
MyAnnotation[] myAnnotations();
}
使用
@MyAnnotation2(myAnnotations={@MyAnnotation(name="注解一",sex="女"),@MyAnnotation(name="注解二",sex="男")})classDemo2{
@MyAnnotation2(myAnnotations={@MyAnnotation(name="注解一",sex="女"),@MyAnnotation(name="注解二",sex="男")})public voidhello(){
System.out.println("hello");
}
}
情形三:特殊的注解属性value
定义
public @interfaceMyAnnotatio