首页 > 通过反射执行get、set方法

通过反射执行get、set方法

Class clazz = sourceObj.getClass();

1、获取所有属性

BeanInfo beanInfo = Introspector.getBeanInfo(clazz);

PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();

2、获取指定属性

PropertyDescriptor pd = new PropertyDescriptor(fieldName, clazz);

Method getMethod = pd.getReadMethod();// 获得get方法

Object val = getMethod.invoke(sourceObj);// 执行get方法返回值

Method setMethod = pd.getWriteMethod();//获得set方法

setMethod .invoke(sourceObj, newVal);

 

转载于:https://www.cnblogs.com/BensonHe/p/4401752.html

更多相关:

  • 来源:公众号|计算机视觉工坊(系投稿)作者:仲夏夜之星「3D视觉工坊」技术交流群已经成立,目前大约有12000人,方向主要涉及3D视觉、CV&深度学习、SLAM、三维重建、点云后处理、自动驾驶、CV入门、三维测量、VR/AR、3D人脸识别、医疗影像、缺陷检测、行人重识别、目标跟踪、视觉产品落地、视觉竞赛、车牌识别、硬件选型、学术交流、...

  • 点云PCL免费知识星球,点云论文速读。文章:Real-Time LIDAR-Based Urban Road and Sidewalk Detection for Autonomous Vehicles作者:Ern˝o Horváth  , Claudiu Pozna ,and Miklós Unger编译:点云PCL代码:http...

  • 文章:Semantic Histogram Based Graph Matching for Real-Time Multi-Robot Global Localization in Large Scale Environment作者:Xiyue Guo, Junjie Hu, Junfeng Chen, Fuqin Deng, T...

  • 点云PCL免费知识星球,点云论文速读。文章:Robust Place Recognition using an Imaging Lidar作者:Tixiao Shan, Brendan Englot, Fabio Duarte, Carlo Ratti, and Daniela Rus编译:点云PCL(ICRA 2021)开源代码:...

  • 文章:A Survey of Calibration Methods for Optical See-Through Head-Mounted Displays作者:Jens Grubert , Yuta Itoh, Kenneth Moser编译:点云PCL本文仅做学术分享,如有侵权,请联系删除。欢迎各位加入免费知识星球,获取PD...