当前位置:网站首页>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.结果

边栏推荐
- Longest palindrome substring (dynamic programming)
- PMP证书有没有必要续期?
- Clickhouse (03) how to install and deploy Clickhouse
- Timer创建定时器
- The founder has a debt of 1billion. Let's start the class. Is it about to "end the class"?
- HarmonyOS第四次培训
- Wonderful express | Tencent cloud database June issue
- Scheduledexecutorservice timer
- Techniques d'utilisation de sublime
- The most complete learning rate adjustment strategy in history LR_ scheduler
猜你喜欢

Salesforce 容器化 ISV 场景下的软件供应链安全落地实践

一个酷酷的“幽灵”控制台工具

漏电继电器JOLX-GS62零序孔径Φ100

EGR-20USCM接地故障继电器

使用知云阅读器翻译统计遗传学书籍

When knative meets webassembly
[email protected] Mapping relatio"/>Why JSON is used for calls between interfaces, how fastjson is assigned, fastjson 1.2 [email protected] Mapping relatio
![[question] Compilation Principle](/img/ce/71f8409ba2cebd497bed0210290895.png)
[question] Compilation Principle

基于Bevy游戏引擎和FPGA的双人游戏

LabVIEW在打开一个新的引用,提示内存已满
随机推荐
一个酷酷的“幽灵”控制台工具
ClickHouse(03)ClickHouse怎么安装和部署
局部变量的数组初始化问题
Leetcode (417) -- Pacific Atlantic current problem
Is it necessary to renew the PMP certificate?
DBSync新增对MongoDB、ES的支持
Longest common subsequence (LCS) (dynamic programming, recursive)
JHOK-ZBL1漏电继电器
拿到PMP认证带来什么改变?
DJ-ZBS2漏电继电器
Development thoughts of adding new requirements in secondary development
The execution order of return in JS' try catch finally
U++ 游戏类 学习笔记
《五》表格
AOSP ~binder communication principle (I) - Overview
接口间调用为什么要用json、fastjson怎么赋值的、fastjson [email protected]映射关系问题
QT simple layout box model with spring
高手勿进!写给初中级程序员以及还在大学修炼的“准程序员”的成长秘籍
Addressable pre Download
[QT] custom control loading