// 获取对象中属性的值 public String getFieldValue(String attrName) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { // 转驼峰 String standardField = getStandardField(attrName.toLowerCase()); String getField = "get" + standardField; return this.getClass().getMethod(getField).invoke(this).toString(); } // 获取驼峰字段 private String getStandardField(String field){ int index = field.indexOf("_"); if(index==-1){ return field.substring(0,1).toUpperCase() + field.substring(1); } String prefix = field.substring(0, index); String upperCase = field.substring(index, index + 2).replace("_","").toUpperCase(); String suffix = field.substring(index + 2 ); return getStandardField(prefix + upperCase + suffix); }
标签:index,String,驼峰,获取,substring,field,getStandardField,属性 From: https://www.cnblogs.com/szc1998/p/18087525