返回顶端

Java 注解与反射(三)自定义注解

Java 注解与反射(三)自定义注解

import java.lang.annotation.*;

public class Demo03 {

    @MyAnnotation2(name = "zzz",age = 20)
    public void test(){

    }

    @MyAnnotation3("省略了value=")
    public void test2(){

    }
}

@Target(value = {ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2{

    //定义的方法为参数
    String name() default "";
    int age() default 0;
    String[] schools() default {"清华大学","北京大学"};

}

@Target(value = {ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation3{

    //默认参数必须为value才可省略
    String value();
}

posted @ 2021-04-03 16:13  EEEEEEEric  阅读(38)  评论(0)    收藏  举报