不知道反射能不能满足你的需求
package test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Test {
private int value;
public void setValue(int value) {
this.value = value;
}
public int getValue() {
System.out.println("call getValue...");
return this.value;
}
public static void main(String[] args) throws InterruptedException {
Test t = new Test();
t.setValue(123);
try {
for(Method method:t.getClass().getDeclaredMethods()){
if(method.getName().startsWith("get")){
System.out.println(method.invoke(t));
}
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}