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

边栏推荐
猜你喜欢

What changes will PMP certification bring?

SQL injection cookie injection

Inventory host list in ansible (I wish you countless flowers and romance)

Auto. JS get all app names of mobile phones

sublime使用技巧

Harmonyos fourth training

漏电继电器LLJ-100FS

Leetcode (46) - Full Permutation

Leetcode(417)——太平洋大西洋水流问题

window定时计划任务
随机推荐
【最佳网页宽度及其实现】「建议收藏」
ThinkPHP关联预载入with
Addressable 预下载
【PHP SPL笔记】
接口间调用为什么要用json、fastjson怎么赋值的、fastjson [email protected]映射关系问题
最长回文子串(动态规划)
Longest palindrome substring (dynamic programming)
Torch optimizer small parsing
QSlider of QT control style series (I)
PMP证书有没有必要续期?
高手勿进!写给初中级程序员以及还在大学修炼的“准程序员”的成长秘籍
【opencv】图像形态学操作-opencv标记不同连通域的位置
ASP. Net MVC - resource cannot be found error - asp Net MVC – Resource Cannot be found error
SQL injection - secondary injection and multi statement injection
批量归一化(标准化)处理
Mysql database (basic)
Why is the salary of test and development so high?
漏电继电器JELR-250FG
Array initialization of local variables
项目经理如何凭借NPDP证书逆袭?看这里