当前位置:网站首页>Notes...
Notes...
2022-07-07 19:34:00 【Cold Snowflakes】
annotation Annotation Is a reference data type , After compiling, it is also generated xxx.class file .
@Target(value = {
ElementType.METHOD,ElementType.CONSTRUCTOR}) // Appoint Where to mark
@Retention(RetentionPolicy.SOURCE) // Indicates that it will not compile into class file
@Retention(RetentionPolicy.CLASS) // The final compilation is class file
@Retention(RetentionPolicy.RUNTIME) // The final compilation is class file , And can be read by reflection mechanism
@Deprecated // Annotation is out of date , There's a better plan ( It just plays a role of marking information , The marked things can still be used )
Custom annotation
public @interface MyAnnotation {
/** * Can be in The attribute is defined in the annotation * It looks like a method , It is actually an attribute * @return */
String name();
/** * color property */
String color();
/** * Age attribute */
int age() default 25; // Specify default
}
// When using annotations , You must assign values to attributes that do not have default values
@MyAnnotation(name = "xxx",color = "blue")
public class lambdademo {
public static void main(String[] args){
}
}
If there is only one attribute in the annotation , And the attribute is called value Words , When using this annotation , Assign values to properties , You can leave the property name unspecified .
If an attribute in the annotation is an array type , And use this annotation , When assigning values to this attribute , There is only one value in braces , Then braces can be omitted .
public @interface Target {
ElementType[] value();
}
// When you use it , There is only one element in braces , Braces can be omitted
@Target(ElementType.METHOD) Equate to @Target({
ElementType.METHOD})
Reflection extraction annotation
precondition
@Retention(RetentionPolicy.RUNTIME) // Must be marked Runtime, Can be read by the reflection mechanism
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");
// Get the marked on the class MyAnnotation
if(cls.isAnnotationPresent(MyAnnotation.class)){
System.out.println("====== Marked on the class ======");
MyAnnotation ano = cls.getAnnotation(MyAnnotation.class);
System.out.println(ano);
String color = ano.color(); // obtain color Property value
System.out.println(color);
}
// Get the marked on the attribute MyAnnotation
Field[] fields = cls.getDeclaredFields();
for(Field f : fields){
if(f.isAnnotationPresent(MyAnnotation.class)){
System.out.println("====== Marked on the attribute ======");
System.out.println(" Marked attributes " + f.getName());
System.out.println(f.getAnnotation(MyAnnotation.class));
}
}
}
What's the use of annotations ?
Marking of the program , You can get this mark through reflection .
The program can judge if 【 class / Method / Properties and so on 】 There is this tag information on it , Just do whatever , without , Do what .
边栏推荐
- Jerry's headphones with the same channel are not allowed to pair [article]
- How to implement safety practice in software development stage
- 解决远程rviz报错问题
- Former richest man, addicted to farming
- Make insurance more "safe"! Kirin Xin'an one cloud multi-core cloud desktop won the bid of China Life Insurance, helping the innovation and development of financial and insurance information technolog
- [tpm2.0 principle and Application guide] Chapter 9, 10 and 11
- [Verilog advanced challenge of Niuke network question brushing series] ~ multi bit MUX synchronizer
- [tpm2.0 principle and Application guide] Chapter 16, 17 and 18
- 一张图深入的理解FP/FN/Precision/Recall
- 2022如何评估与选择低代码开发平台?
猜你喜欢
随机推荐
Number - number (Lua)
99% of people don't know that privatized deployment is also a permanently free instant messaging software!
LC:字符串转换整数 (atoi) + 外观数列 + 最长公共前缀
how to prove compiler‘s correctness
2022.07.05
[RT thread env tool installation]
ant desgin 多选
位运算介绍
PV静态创建和动态创建
【RT-Thread env 工具安装】
State mode - Unity (finite state machine)
杰理之关于 TWS 配对方式配置【篇】
歌单11111
2022.07.05
[Verilog advanced challenge of Niuke network question brushing series] ~ multi bit MUX synchronizer
谷歌seo外链Backlinks研究工具推荐
R语言ggplot2可视化:使用ggpubr包的ggstripchart函数可视化分组点状条带图(dot strip plot)、设置position参数配置不同分组数据点的分离程度
PMP对工作有益吗?怎么选择靠谱平台让备考更省心省力!!!
杰理之快速配对,不支持取消配对【篇】
一张图深入的理解FP/FN/Precision/Recall









