编程技术分享平台

网站首页 > 技术教程 正文

如何自定义 Annotation(如何自定义手机铃声)

xnh888 2024-10-19 15:59:02 技术教程 18 ℃ 0 评论

一:自定义 Annotation

1、定义新的 Annotation 类型使用 @interface 关键字

2、Annotation 的成员变量在Annotation定义中以无参数方法的形式来声明. 其方法名和返回值定义了该成员的名字和类型.

3、可以在定义 Annotation 的成员变量时为其指定初始值, 指定成员变量的初始值可使用 default 关键字

4、没有成员定义的 Annotation 称为标记; 包含成员变量的 Annotation 称为元数据 Annotation

自定义

二:JDK 的元 Annotation

1、JDK 的元 Annotation 用于修饰其他 Annotation 定义

2、JDK5.0提供了专门在注解上的注解类型,分别是:

Retention

Target

Documented

Inherited

1、Retention

1、@Retention: 只能用于修饰一个 Annotation 定义, 用于指定该 Annotation 可以保留多长时间

2、@Rentention 包含一个 RetentionPolicy 类型的成员变量, 使用

2、Target

1、@Target:用于修饰 Annotation 定义、 用于指定被修饰的 Annotation 能用于修饰哪些程序元素

2、@Target 也包含一个名为 value 的成员变量.

3、Documented

1、@Documented: 用于指定被该元 Annotation 修饰的 Annotation 类将被 javadoc 工具提取成文档.

2、定义为Documented的注解必须设置Retention值为RUNTIME。

4、Inherited

1、@Inherited: 被它修饰的 Annotation 将具有继承性.如果某个类使用了被 @Inherited 修饰的 Annotation 则其子类将自动具有该注解

2、实际应用中,使用较少

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表