当前位置:网站首页>遍历未知类型对象属性
遍历未知类型对象属性
2022-06-10 11:57:00 【wfsm】
引用:https://blog.csdn.net/ExceptionalBoy/article/details/79203465
- 反射获取field
- 反射获取getXXX() 方法
@Test
public void test02 () throws IllegalAccessException {
Object cc = new Student(1, "cc");
// printBeanField(cc);
printBeanField2(cc);
}
private void printBeanField(Object obj) throws IllegalAccessException {
// 判断对象是否为空
if (obj != null){
Field[] fields = obj.getClass().getDeclaredFields();
if (fields != null && fields.length !=0){
for (Field f : fields) {
f.setAccessible(true);
Object fieldValue = f.get(obj);
String fieldName = f.getName();
System.out.println(fieldName+"---"+fieldValue);
}
}
}
}
private void printBeanField2(Object obj) {
if (obj != null){
Method[] methods = obj.getClass().getDeclaredMethods();
if (methods != null && methods.length>0){
Arrays.stream(methods).filter((item)->item.getName().startsWith("get")).forEach(item->{
try {
item.setAccessible(true);
String methodName = item.getName();
Object methodValue = item.invoke(obj);
System.out.println(methodName+"--"+methodValue);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
});
}
}
}
边栏推荐
- web设计与开发,高效web开发
- Xijun data cooperates with Alibaba cloud to obtain Alibaba cloud product integration certification!
- JS将阿拉伯数字翻译成中文的大写数字、JS将数字转换为大写金额(整理)
- 怎样将PDF作为矢量图插入PPT中
- 线性代数的本质4 矩阵乘法与线性复合变换
- 0509-130 Symbol resolution failed for /oracle/app/oracle/12.2.0/db_ 1/lib/libons. So problem handling
- Use of indexeddb database
- list.remove(index)返回flase,移除失败
- Start from 0 to build a high-performance R & D full stack team
- Essential 5 determinants of linear algebra
猜你喜欢

Using SQLite3 database in embedded Linux

Fun face recognition software

【限时】,干货满满

一站式机器学习开业平台 MLflow 怎么样?

Cvpr22 oral | Hong Kong Chinese proposed transrank: sequencing loss + self supervision =sota

4.25 million budget bidding: centralized procurement of onsite maintenance services for Oracle, teledb, telepg and other core system databases of Jiangsu Telecom

Product milestones in May 2022

并发bug之源(一)-可见性

On array

The teaching staff at station B will teach you how to use the most complete record of the mask detection items of yolov5 (in-depth learning / target detection / pytorch)
随机推荐
“易 +”开源计划丨基于 WebRTC 的低延时播放器设计和实践
共筑数字经济可信底座|2022可信数据库峰会即将召开
B站教學 手把手教你使用YOLOV5之口罩檢測項目 最全記錄詳解 ( 深度學習 / 目標檢測 / pytorch )
“减负”,让“猪”可以飞得更高
搜狐员工遭遇工资补助诈骗 黑产与灰产有何区别 又要如何溯源?
AGCO AI frontier promotion (6.10)
使用Joern处理大量文件,生成PDG速度很慢的解决方法
"Duty" is waiting for you| Book planning and editing (Conference Proceedings)
并发bug之源(一)-可见性
Green Alliance database firewall (das-fw) has obtained Kunpeng validated certification
【严选】,真题解析
Remote access to tensorboard
Using SQLite3 database in embedded Linux
Transfomer各组件与Pytorch
『忘了再学』Shell基础 — 29、AWK内置变量
国际档案日|瀚高数据库以科技赋能数字档案建设
Lvs+keepalived highly available cluster
Introduction: from the origin of etcd name to installation, deployment and usage posture
向上滑动导航固定头部
Flutter库推荐Sizer 可帮助您轻松创建响应式 UI