一个PO类public class ExamplePO{
  private String row1;
  private String row2;
  ...
  private String row100;
  //然后是get和set方法
}在另一个方法里面,我new了一个ExamplePO的对象example,然后需要用一个for循环来遍历调用example的所有get方法,而且要按照1->100的顺序。
求高手来助,分不多了。遍历Java对象

解决方案 »

  1.   

    我试过,但是那个只是取得了方法名,不能用这个对象来调用啊。而且不知道为什么他获得的get方式是从100->1,顺序也不是我想要的
      

  2.   


    public class ExamplePO {
    private String row1;
    private String row2;
    private String row3;
    private String row100;

    public String getRow1() {
    System.out.println("getRow1()");
    return row1;
    }

    public String getRow2() {
    System.out.println("getRow2()");
    return row2;
    } public String getRow3() {
    System.out.println("getRow3()");
    return row3;
    } public String getRow100() {
    System.out.println("getRow100()");
    return row100;
    } public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException {
    Method[] methods = ExamplePO.class.getDeclaredMethods();
    for(Method method : methods){
    String methodName = method.getName();
    if(methodName != null && methodName.startsWith("get")){
    method.invoke(ExamplePO.class.newInstance(), null);
    }
    }
    }
    }按1-100顺序声明row