自定义注解

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 "";
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值