/** * @Author MWZHYF * @Description // 产生对象字段 * @Date 15:33 2019/4/25 * @Param [obj] * @return java.lang.String[] **/ public static String[] generateObjAttr(Object obj) { Class classObj = (Class)obj.getClass(); Field[] fields = classObj.getDeclaredFields(); String[] title = new String[fields.length]; for (int i = 0; i< fields.length; i++ ) { title[i] = fields[i].getName(); } return title; } /** * @Author MWZHYF * @Description // 产生 属性值 * @Date 15:33 2019/4/25 * @Param [fieldName, obj] * @return java.lang.Object **/ public static Object generateAttrValue(String fieldName, Object obj) { try { String firstLetter = fieldName.substring(0, 1).toUpperCase(); String getter = "get" + firstLetter + fieldName.substring(1); Method method = obj.getClass().getMethod(getter, new Class[] {}); Object value = method.invoke(obj, new Object[] {}); return value; } catch (Exception e) { e.printStackTrace(); return null; } }
java 反射,根据类获取 属性名字和值
最新推荐文章于 2024-02-29 20:53:11 发布