当前位置:网站首页>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 .
边栏推荐
猜你喜欢

Empowering smart power construction | Kirin Xin'an high availability cluster management system to ensure the continuity of users' key businesses

Kirin Xin'an joins Ningxia commercial cipher Association

【RT-Thread env 工具安装】

项目经理『面试八问』,看了等于会了

Kirin Xin'an cloud platform is newly upgraded!

cmd命令进入MySQL时报服务名或者命令错误(傻瓜式教学)

索引总结(突击版本)

In 2021, the national average salary was released. Have you reached the standard?

2022.07.04

2022.07.05
随机推荐
LeetCode 648(C#)
实训九 网络服务的基本配置
Jerry's headphones with the same channel are not allowed to pair [article]
Initial experience of cache and ehcache "suggestions collection"
Review of network attack and defense
杰理之开机自动配对【篇】
Chief technology officer of Pasqual: analog quantum computing takes the lead in bringing quantum advantages to industry
歌单11111
Install mysql8 for Linux X ultra detailed graphic tutorial
What does "true" mean
Policy mode - unity
how to prove compiler‘s correctness
超分辨率技术在实时音视频领域的研究与实践
从39个kaggle竞赛中总结出来的图像分割的Tips和Tricks
Former richest man, addicted to farming
R语言ggplot2可视化:使用ggpubr包的ggqqplot函数可视化QQ图(Quantile-Quantile plot)
LeetCode 535(C#)
Draw squares with Obama (Lua)
In 2021, the national average salary was released. Have you reached the standard?
【Confluence】JVM内存调整