//自定义注解
public class Test01 {
@MyAnnotation(name="潘潘") //默认空,没有默认必须填写值
public void test(){
}
@MyAnnotation2("潘潘")
public void test1(){
}
}
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation{
//参数
String name() default ""; //默认为空,用的时候可以不填参数
int age() default 0;
int id() default -1; //默认值-1表示不存在
String[] schools() default {"社会大学","天天大学"};
}
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2{
String value();
}
注解与反射的练习
最新推荐文章于 2025-12-15 13:40:30 发布
这篇博客介绍了如何在Java中使用自定义注解和元注解,例如`@MyAnnotation`和`@MyAnnotation2`。`@MyAnnotation`包含name、age和id等属性,`@MyAnnotation2`只有一个value属性。这些注解可以应用于类和方法级别,并在运行时被读取,展示了Java的元编程能力。
2732

被折叠的 条评论
为什么被折叠?



