好吧,如果你真的想要,你可以打电话:public class Student {
private String name;
private int age;
public String getName(){
System.out.println("getName called");
return name;
}
public int getAge(){
System.out.println("getAge called");
return age;
}}public class Test {
public static void main(String[] args) throws Exception {
Student s=new Student();
System.out.println(s.getClass().getMethod("getName").invoke(s));
System.out.println(s.getClass().getMethod("getAge").invoke(s));
}}
产生getName callednullgetAge called0
如果你有"name"或"age"在字符串中something,剩下的任务就是做类似的事情"get"+something.substring(0,1).toUpperCase()+something.substring(1)