当前位置:网站首页>Annotation初体验
Annotation初体验
2022-07-06 23:23:00 【菜鸟xiaowang】
1.定义注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Person {
String name() default "abc";
int age() default 20;
}
2.使用注解
public class Student {
@Person(name = "王xx",age = 30)
public Object person;
}3.处理注解
public class PersonUtils {
public static void getPersonInfo(Class<?> clazz){
for (Field declaredField : clazz.getDeclaredFields()) {
if (declaredField.isAnnotationPresent(Person.class)){
Person person = declaredField.getAnnotation(Person.class);
System.out.println("name:"+person.name()+"age"+person.age());
}
}
}
}4.测试
PersonUtils.getPersonInfo(Student.class);5.结果

边栏推荐
- NPDP产品经理认证,到底是何方神圣?
- 线程同步的两个方法
- Operand of null-aware operation ‘!‘ has type ‘SchedulerBinding‘ which excludes null.
- Safe landing practice of software supply chain under salesforce containerized ISV scenario
- 漏电继电器JOLX-GS62零序孔径Φ100
- QT simple layout box model with spring
- sublime使用技巧
- How can professional people find background music materials when doing we media video clips?
- Redis如何实现多可用区?
- qt 简单布局 盒子模型 加弹簧
猜你喜欢
随机推荐
批量归一化(标准化)处理
【PHP SPL笔记】
Window scheduled tasks
基于Bevy游戏引擎和FPGA的双人游戏
Dynamically generate tables
Leetcode(417)——太平洋大西洋水流问题
Leetcode (417) -- Pacific Atlantic current problem
Why JSON is used for calls between interfaces, how fastjson is assigned, fastjson 1.2 [email protected] Mapping relatio
漏电继电器JELR-250FG
Leetcode longest public prefix
The sooner you understand the four rules of life, the more blessed you will be
Longest common subsequence (LCS) (dynamic programming, recursive)
【问道】编译原理
使用知云阅读器翻译统计遗传学书籍
Ansible reports an error: "MSG": "invalid/incorrect password: permission denied, please try again“
Leetcode(46)——全排列
npm ERR! 400 Bad Request - PUT xxx - “devDependencies“ dep “xx“ is not a valid dependency name
Is PMP really useful?
做自媒体视频剪辑,专业的人会怎么寻找背景音乐素材?
How does redis implement multiple zones?








