209.自定义注解
package com.bjsxt.test.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(value={ElementType.METHOD,ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface SxtAnnotation01 { String studentName() default ""; int age() default 0; int id() default -1; //String indexOf("abc") -1 String[] schools() default {"清华大学","北京上学堂"}; }
注解1
SxtAnnotation01 范围是可以使用在类的方法
ElementType.METHOD以及对类做注解
ElementType.TYPE
注解运行的有效期是:
@Retention(RetentionPolicy.RUNTIME)
定义可三个字段
studentName、age id,已经schools,每个字段都使用了一个默认值
package com.bjsxt.test.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(value={ElementType.METHOD,ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface SxtAnnotation02 { String value(); }
有了上面的注解,现在我们这样使用注解了
package com.bjsxt.test.annotation; /** * 测试自定义注解的使用 * */ @SxtAnnotation01 public class Demo02 { @SxtAnnotation01(age=19,studentName="老高",id=1001, schools={"北京大学","北京航空航天大学"}) public void test(){ } @SxtAnnotation02("aaaa") public void test2(){ } }
这里对应注解 @SxtAnnotation02("aaaa"),为啥能这样写而不是写成 @SxtAnnotation02(value="aaaa"),这里因为SxtAnnotation02中只有一个成员变量value,所以我们就可以这样写
posted on 2017-08-01 13:39 luzhouxiaoshuai 阅读(116) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!