0

到目前为止,我已经像其他任何方法一样编写我的方法(并且一切正常),但这有一个问题。代码对数据库执行选择查询并创建 MyObject 类型的 ArrayList:

MyObject current;
ArrayList<MyObject> res = new ArrayList<MyObject>();
Connection conn=ConnectionManager.getConnection();
Statement stm = conn.createStatement();         
ArrayList<MyObject> res = new ArrayList<MyObject>();
Result set rs = stm.executeQuery(QUERY);

while(rs.next()) {
    current = new MyObject();
    current.setField1(rs.getInt(1));
    current.setField2(rs.getInt(2));
    System.out.println(current.getField1+" "+current.getField2());
    res.add(current);
}

(MyObject 扩展了一个包含方法 setField2(int a) 的超类)。接下来的问题。如果我直接在数据库上执行查询,我会得到以下结果:

0 5000

1 5000

2 6000

但是对 Java 代码执行查询返回

0 6000

1 6000

2 6000

MyObject 和 MySuperObject 类

public class MyObject extends MySuperObject {
    potected int field1;
    public MyObject() {
        super();
    }
    @Override
    public int getField1() {
        return this.field1;
    }
    public void setField1(int f) {
        this.field1 = f;
    }
}


public class MySuperObject {
    potected int field1, field2;
    public MySuperObject() {
    }

    public int getField1() {
        return this.field1;
    }
    public void setField1(int f) {
        this.field1 = f;
    }

    public void setField2(int f) {
        this.field2 = f;
    }

    public int getField2() {
       return this.field2;
    }
}
4

0 回答 0