当前位置:网站首页>注解。。。
注解。。。
2022-07-07 17:29:00 【Cold Snowflakes】
注解Annotation是一种引用数据类型,编译之后也是生成xxx.class文件。
@Target(value = {
ElementType.METHOD,ElementType.CONSTRUCTOR}) // 指定 可以标注的位置
@Retention(RetentionPolicy.SOURCE) // 表示最终不会编译成 class 文件
@Retention(RetentionPolicy.CLASS) // 最终编译为 class 文件
@Retention(RetentionPolicy.RUNTIME) // 最终编译为 class 文件, 并且可以被反射机制读取
@Deprecated // 标注已过时, 有更好的方案 ( 只是起一个标注信息的作用, 被标注的东西仍然还可以用 )
自定义注解
public @interface MyAnnotation {
/** * 可以在 注解中定义属性 * 看着像个方法, 实际为属性 * @return */
String name();
/** * 颜色属性 */
String color();
/** * 年龄属性 */
int age() default 25; // 指定默认值
}
// 使用注解的时候, 必须给没有设置默认值的属性赋值
@MyAnnotation(name = "xxx",color = "blue")
public class lambdademo {
public static void main(String[] args){
}
}
如果注解中只有一个属性,并且属性名叫 value
的话,则使用该注解的时候,为属性赋值,可以不指定属性名。
如果注解中的某个属性是一个数组类型,且使用该注解,为该属性赋值的时候,大括号里只有一个值,则大括号可以省略。
public @interface Target {
ElementType[] value();
}
// 使用的时候, 大括号里只有一个元素, 大括号可以省略
@Target(ElementType.METHOD) 等同于 @Target({
ElementType.METHOD})
反射提取注解
precondition
@Retention(RetentionPolicy.RUNTIME) // 必须要标注 Runtime, 才可以被反射机制读取
public @interface MyAnnotation {
String name();
String color();
}
Experimental subjects
@MyAnnotation(name = "on type", color = "red")
public class Cat {
@MyAnnotation(name = "on field", color = "blue")
private String name;
private Integer age;
private Integer sex;
}
Extraction by reflection
public static void main(String[] args) throws ClassNotFoundException {
Class<?> cls = Class.forName("com.itheima.Cat");
// 得到类上标注的 MyAnnotation
if(cls.isAnnotationPresent(MyAnnotation.class)){
System.out.println("======标注在类上的======");
MyAnnotation ano = cls.getAnnotation(MyAnnotation.class);
System.out.println(ano);
String color = ano.color(); // 得到 color 属性值
System.out.println(color);
}
// 得到属性上标注的 MyAnnotation
Field[] fields = cls.getDeclaredFields();
for(Field f : fields){
if(f.isAnnotationPresent(MyAnnotation.class)){
System.out.println("======标注在属性上的======");
System.out.println("被标注的属性 " + f.getName());
System.out.println(f.getAnnotation(MyAnnotation.class));
}
}
}
注解有啥用?
对程序的标记,通过反射可以获取到这个标记。
程序可以判断如果【类/方法/属性等等】上面有这个标记信息,就去做什么什么,如果没有,做什么什么。
边栏推荐
猜你喜欢
2022上半年朋友圈都在传的10本书,找到了
索引总结(突击版本)
AD域组策略管理
Seize Jay Chou
Version 2.0 of tapdata, the open source live data platform, has been released
PV static creation and dynamic creation
In the first half of 2022, I found 10 books that have been passed around by my circle of friends
2022.07.04
2022如何评估与选择低代码开发平台?
爬虫实战(七):爬王者英雄图片
随机推荐
“本真”是什么意思
Key points of anti reptile: identifying reptiles
[mime notes]
杰理之关于 TWS 声道配置【篇】
Numpy——axis
LeetCode 890(C#)
Netease Yunxin participated in the preparation of the standard "real time audio and video service (RTC) basic capability requirements and evaluation methods" issued by the Chinese Academy of Communica
Review of network attack and defense
鸿蒙智能家居【1.0】
谷歌seo外链Backlinks研究工具推荐
LeetCode 535(C#)
Kirin Xin'an joins Ningxia commercial cipher Association
2022.07.02
ant desgin 多选
how to prove compiler‘s correctness
Business experience in virtual digital human
怎么在手机上买股票开户 股票开户安全吗
Initial experience of cache and ehcache "suggestions collection"
博睿数据入选《2022爱分析 · IT运维厂商全景报告》
Install mysql8 for Linux X ultra detailed graphic tutorial