你可以做这样的事情。您可以使此代码更通用,以便将其用于循环字段:
Class aClass = f.getClass();Class[] paramTypes = new Class[1];paramTypes[0] = String.class; // get the actual param typeString methodName = "set" + fieldName; // fieldName StringMethod m = null;try { m = aClass.getMethod(methodName, paramTypes);} catch (NoSuchMethodException nsme) { nsme.printStackTrace();}try { String result = (String) m.invoke(f, fieldValue); // field value System.out.println(result);} catch (IllegalAccessException iae) { iae.printStackTrace();} catch (InvocationTargetException ite) { ite.printStackTrace();}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)