当前位置:网站首页>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 .
边栏推荐
- 国内首次!这家中国企业的语言AI实力被公认全球No.2!仅次于谷歌
- 杰理之按键发起配对【篇】
- Solve the error reporting problem of rosdep
- MySQL、sqlserver oracle数据库连接方式
- R语言使用ggplot2函数可视化需要构建泊松回归模型的计数目标变量的直方图分布并分析构建泊松回归模型的可行性
- 杰理之开机自动配对【篇】
- Tips and tricks of image segmentation summarized from 39 Kabul competitions
- 最长公共前缀(leetcode题14)
- LeetCode1051(C#)
- 歌单11111
猜你喜欢

Chief technology officer of Pasqual: analog quantum computing takes the lead in bringing quantum advantages to industry
![[RT thread env tool installation]](/img/bc/9b39651d40a240f0893200793f67e9.png)
[RT thread env tool installation]

How to estimate the value of "not selling pens" Chenguang?

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

# 欢迎使用Markdown编辑器

Matplotlib drawing 3D graphics

多个kubernetes集群如何实现共享同一个存储
![[tpm2.0 principle and Application guide] Chapter 9, 10 and 11](/img/7f/0d4d91142bc3d79ea445a8f64afba7.png)
[tpm2.0 principle and Application guide] Chapter 9, 10 and 11

ES6 note 1

Kirin Xin'an joins Ningxia commercial cipher Association
随机推荐
AI写首诗
Classification and application of enterprise MES Manufacturing Execution System
浏览积分设置的目的
索引总结(突击版本)
AI writes a poem
LeetCode 890(C#)
2022.07.02
R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图、设置palette参数自定义不同水平小提琴图的填充色、add参数在小提琴图添加箱图
PV静态创建和动态创建
最长公共前缀(leetcode题14)
Initial experience of cache and ehcache "suggestions collection"
CMD command enters MySQL times service name or command error (fool teaching)
R语言ggplot2可视化:使用ggpubr包的ggdensity函数可视化分组密度图、使用stat_overlay_normal_density函数为每个分组的密度图叠加正太分布曲线
炒股如何开户?请问一下手机开户股票开户安全吗?
L1-027 rental (Lua)
Pasqal首席技术官:模拟量子计算率先为工业带来量子优势
AD域组策略管理
2022.07.05
PMP对工作有益吗?怎么选择靠谱平台让备考更省心省力!!!
Longest common prefix (leetcode question 14)