首页 > 其他 > 详细

自定义注解

时间:2021-06-13 01:02:50      阅读:21      评论:0      收藏:0      [点我收藏+]
package annotation;

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

//自定义注解
public class Test03 {
//注解可以显示赋值,如果没有默认值,我们就必须给注解赋值
@MyAnnotation2(name = "aaa",schools = {"哈工大,哈理工"})
public void test(){}
@MyAnnotation3("秦姜")
public void test2(){}
}

//需要元注解
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2{
//注解的参数:参数类型+参数名();
String name() default "";
int age() default 0;
int id() default -1;//如果默认值为-1,代表不存在
String[] schools() default {"西部开源","清华大学"};
}
//需要元注解
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation3{
String value();//当注解只有一个参数的时候 建议使用value()当参数名字,因为 当使用的时候就可以省略参数名
}

自定义注解

原文:https://www.cnblogs.com/nigustudent/p/14878794.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!