1.Anntation的格式
2.实例
public @interface MyAnnotation {
}
@MyAnnotation
public class MyAnnotationTest {
}
3.接收内容
public @interface MyAnnotationSingleParam {
public String value();//接收设置的内容
}
//必须设置参数 或者MyAnnotationSingleParam("suhao")
@MyAnnotationSingleParam(value="suhao")
public class UseMyAnnotationSingleParam {
}
4.默认值
public @interface MyAnnotationDefaultValue {
public String key() default "com.qunar" ;
public String value() default "suhao";
}
@MyAnnotationDefaultValue
public @interface UseMyAnnotationDefaultValue {
}
5.接收数组
public @interface MyAnnotationArrayParam {
public String[] value();//接收设置的内容
}
@MyAnnotationArrayParam(value={"sh","suhao"})
public class UseMyAnnotationArrayParam {
}
6.可以使用枚举来限定取值
public enum MyName {
qunar, sh, suhao;
}
public @interface UseMyName {
public MyName name() default MyName.sh;
}
@UseMyName(name = MyName.qunar)//只能充固定的枚舉中取內容
public class UseMyNameAnnotation {
}