public static void main(String[] args){
String setMethodName = "setinstitution";
String currCellString = "pending";
Class c;
try {
c = Class.forName("User");
Object o = c.newInstance();
Method[] methods = c.getMethods();
for (Method method : methods) {
String methodName = method.getName().toLowerCase();
System.out.println("++The current Method name is: " + methodName);
if (methodName.equals(setMethodName)){
method.invoke(o, currCellString);
System.out.println("-------Sucessfully " + setMethodName);
break;
}
}
System.out.println("@@@" + ((User)o).getInstitution());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
另一种高效率的方法
String currCellString = "pending";
Class c = Class.forName("test");
Object o = c.newInstance();
Method method = c.getMethod("setInstitution", String.class);
method.invoke(o, currCellString);
System.out.println("@@@" + ((User)o).getInstitution());
related url: http://msdn.microsoft.com/en-us/library/aa990552%28v=vs.80%29.aspx
String setMethodName = "setinstitution";
String currCellString = "pending";
Class c;
try {
c = Class.forName("User");
Object o = c.newInstance();
Method[] methods = c.getMethods();
for (Method method : methods) {
String methodName = method.getName().toLowerCase();
System.out.println("++The current Method name is: " + methodName);
if (methodName.equals(setMethodName)){
method.invoke(o, currCellString);
System.out.println("-------Sucessfully " + setMethodName);
break;
}
}
System.out.println("@@@" + ((User)o).getInstitution());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
另一种高效率的方法
String currCellString = "pending";
Class c = Class.forName("test");
Object o = c.newInstance();
Method method = c.getMethod("setInstitution", String.class);
method.invoke(o, currCellString);
System.out.println("@@@" + ((User)o).getInstitution());
related url: http://msdn.microsoft.com/en-us/library/aa990552%28v=vs.80%29.aspx