素材巴巴 > 程序开发 >

从对象中动态获取属性值

程序开发 2023-09-03 20:59:06
/*** 从对象中动态获取属性值* @param obj 对象* @param name 要获取的属性值* @return*/
 private static Object getObjectInfo(Object obj,String name){Object value = new Object();try {Field field = obj.getClass().getDeclaredField(name);field.setAccessible(true);value=field.get(obj);}catch (Exception e){e.getStackTrace();}return value;
 }
public static void main(String[] args){CatPo catPo = CatPo.builder().color("蓝色").build();String color = String.valueOf(getObjectInfo(catPo,"color"));System.out.println(color);
 }

结果:

 


标签:

素材巴巴 Copyright © 2013-2021 http://www.sucaibaba.com/. Some Rights Reserved. 备案号:备案中。