我正在浏览官方 db4o 教程的部分内容,并且正在尝试修改它们为您提供的用于运行本机查询的代码:
//the original
List<Pilot> pilots = db.query(new Predicate<Pilot>() {
public boolean match(Pilot pilot) {
return pilot.getPoints() == 100;
}
});
//modified
List<Pilot> pilots = db.query(new Predicate<Pilot>() {
public boolean match(Pilot pilot) {
return pilot.getGames() >= 100;
}
});
我已将此添加到他们的 Pilot 课程中:
//in declarations
private ArrayList<String> games;
//modified constructors
public Pilot() {
this.name=null;
this.points=0;
}
public Pilot(String name,int points) {
this.name=name;
this.points=points;
this.games = new ArrayList<String>();
int numGames = (int) (Math.random() * 1000 + 1);
for(int i=0;i<numGames;i++) {
this.games.add(name=" vs Computer");
}
}
//new method
public int getGames() {
return games.size();
}
我已经使用第二个构造函数填充了一个包含 500 个对象的数据库,并且使用 OME eclipse 插件,数据库中的所有数据看起来都是正确的。我已经测试了 getGames() 并且它按预期工作。
我的问题是,当我运行修改后的查询时,它返回数据库中的所有对象,我不明白为什么。我尝试更改查询以包含更标准的如果为真,否则为假结构,并更改查询以包括需要一定数量的点无济于事。无论我做什么,它似乎总是评估 (pilot.getGames() >= 100) 为真。
谁能帮我理解为什么?