当前位置:网站首页>注解。。。
注解。。。
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));
}
}
}
注解有啥用?
对程序的标记,通过反射可以获取到这个标记。
程序可以判断如果【类/方法/属性等等】上面有这个标记信息,就去做什么什么,如果没有,做什么什么。
边栏推荐
- In 2021, the national average salary was released. Have you reached the standard?
- Big Ben (Lua)
- Longest common prefix (leetcode question 14)
- Kirin Xin'an cloud platform is newly upgraded!
- 杰理之相同声道的耳机不允许配对【篇】
- Chief technology officer of Pasqual: analog quantum computing takes the lead in bringing quantum advantages to industry
- AI writes a poem
- 一张图深入的理解FP/FN/Precision/Recall
- PV静态创建和动态创建
- 99% of people don't know that privatized deployment is also a permanently free instant messaging software!
猜你喜欢
Responsibility chain model - unity
谷歌seo外链Backlinks研究工具推荐
Kunpeng developer summit 2022 | Kirin Xin'an and Kunpeng jointly build a new ecosystem of computing industry
Version 2.0 of tapdata, the open source live data platform, has been released
How to estimate the value of "not selling pens" Chenguang?
Install mysql8 for Linux X ultra detailed graphic tutorial
转置卷积理论解释(输入输出大小分析)
ES6 note 1
Scientists have observed for the first time that the "electron vortex" helps to design more efficient electronic products
[information security laws and regulations] review
随机推荐
实训九 网络服务的基本配置
网信办公布《数据出境安全评估办法》,9 月 1 日起施行
Install mysql8 for Linux X ultra detailed graphic tutorial
LeetCode 890(C#)
LC:字符串转换整数 (atoi) + 外观数列 + 最长公共前缀
Unable to link the remote redis server (solution 100%
For friends who are not fat at all, nature tells you the reason: it is a genetic mutation
ES6 note 1
Scientists have observed for the first time that the "electron vortex" helps to design more efficient electronic products
ES6笔记一
how to prove compiler‘s correctness
杰理之关于 TWS 声道配置【篇】
5billion, another master fund was born in Fujian
企业MES制造执行系统的分类与应用
L1-025 positive integer a+b (Lua)
How to buy stocks on your mobile phone and open an account? Is it safe to open an account
现在股票开户可以直接在网上开吗?安全吗。
怎么在手机上买股票开户 股票开户安全吗
位运算介绍
Research and practice of super-resolution technology in the field of real-time audio and video