myself defined Annotation(自定义注解)

package annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

//自定义注解
public class Test02 {
    @MyAnnotation2(value ="大鑫",id=001)
    public void test(){

    }

}

@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(value = RetentionPolicy.SOURCE)
@interface MyAnnotation2{
    //注解的参数不是写在注解名后,注解名后没有(),只有{}
    //注解的参数写在{}内,格式为:参数类型 参数名();
    //注解的参数可以设置默认指,参数类型 参数名() default 默认指

    String value();
    int age() default 18;// 参数加了默认值后,在写@该注解时,可以省略()中的参数
    int id();// 不加参数,@注解名()括号中的参数要写全, 不然会报错

}

 

posted @ 2022-03-27 15:35  狂神大鑫  阅读(26)  评论(0)    收藏  举报