利用反射获取对象中的值等于x的字段
2024-08-28 03:54:31
Field[] field = behavior.getClass().getDeclaredFields();
for (int i = 0; i < field.length; i++) {
String name = field[i].getName();
name = name.substring(0, 1).toUpperCase() + name.substring(1);//找到首位改成大写形式
String type = field[i].getGenericType().toString();//字段类型
if (type.equals("class java.lang.String")) {
Method m;
String value;
try {
m = behavior.getClass().getMethod("get" + name);
value = (String) m.invoke(behavior);
if (value.equals("x")) {//value是获取到的值
mapdata.put("behavior", name);//name是字段名
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}
}
最新文章
- guava学习--hashing
- Javascript基础系列之(二)变量
- Office2010 pro附+激活工具
- JS闭包(转载加整理)
- lintcode :nth to Last Node In List 链表倒数第n个节点
- Lua学习笔记(二):基本语法
- 软件工程 --- Pair Project: Elevator Scheduler [电梯调度算法的实现和测试]
- webbroswer 后台注入脚本 的方法
- Java 8 表示两个时间点距离
- 华为C语言编程规范笔记1
- 在 Visual Studio 中使用 IntelliTrace 快照功能
- Element.scrollIntoView()
- 什么是联机分析处理(OLAP)
- C++ Primer 笔记——基本内置类型
- RF射频技术的原理
- JS浏览器BOM
- bzoj 5334 数学计算
- HDU 4355——Party All the Time——————【三分求最小和】
- [arc063]F.すぬけ君の塗り絵2
- SpringBoot快速入门01--环境搭建