package annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
@MyAnnotation5(color = Color.RED)
public class MyAnnotation {
public static void main(String[] args) {
@MyAnnotation2(name = "maoxiaodou")
String name = "毛小豆";
@MyAnnotation3("这里写的是value")
int a = 10;
}
}
//当一个注解里面没有任何成员的时候,我们就称这个注解为标记注解
//这样的注解可以修饰类的任何成员
@interface MyAnnotation1{
}
//其他的成员使用这个注解的时候,需要给注解赋值@MyAnnotation2(name = "maoxiaodou")
@interface MyAnnotation2{
String name();
}
//使用注解的时候要给value值 @MyAnnotation3("这里写的是value")
@interface MyAnnotation3{
String value();
}
//给自定义注解添加默认值
@interface MyAnnotation4{
String value() default "有默认值,使用注解的时候就可以不用给值了";
}
enum Color{
RED,BLACK,GREEN;
}
//注解的值可以是枚举类型
//@MyAnnotation5(color = Color.RED)
@interface MyAnnotation5{
Color color();
}
//标记直接在什么情况下使用
@Target(ElementType.CONSTRUCTOR) //标明注解只能在构造方法上使用
@interface MyAnnotation6{
String value() default "";
}
//标记注解的使用环境是一个数组,可以在多种情况下使用
@Target({ElementType.CONSTRUCTOR,ElementType.METHOD})
@interface MyAnnotation7{
String value() default "";
}
package annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
@MyAnnotation5(color = Color.RED)
public class MyAnnotation {
public static void main(String[] args) {
@MyAnnotation2(name = "maoxiaodou")
String name = "毛小豆";
@MyAnnotation3("这里写的是value")
int a = 10;
}
}
//当一个注解里面没有任何成员的时候,我们就称这个注解为标记注解
//这样的注解可以修饰类的任何成员
@interface MyAnnotation1{
}
//其他的成员使用这个注解的时候,需要给注解赋值@MyAnnotation2(name = "maoxiaodou")
@interface MyAnnotation2{
String name();
}
//使用注解的时候要给value值 @MyAnnotation3("这里写的是value")
@interface MyAnnotation3{
String value();
}
//给自定义注解添加默认值
@interface MyAnnotation4{
String value() default "有默认值,使用注解的时候就可以不用给值了";
}
enum Color{
RED,BLACK,GREEN;
}
//注解的值可以是枚举类型
//@MyAnnotation5(color = Color.RED)
@interface MyAnnotation5{
Color color();
}
//标记直接在什么情况下使用
@Target(ElementType.CONSTRUCTOR) //标明注解只能在构造方法上使用
@interface MyAnnotation6{
String value() default "";
}
//标记注解的使用环境是一个数组,可以在多种情况下使用
@Target({ElementType.CONSTRUCTOR,ElementType.METHOD})
@interface MyAnnotation7{
String value() default "";
}