Dashboard > db4o OpenDoc > ... > db4o7.0指南 > 06. Inheritance
db4o OpenDoc Log In   View a printable version of the current page.
06. Inheritance
Added by jeff jie, last edited by cleverpig on Dec 28, 2007  (view change)
Labels: 
(None)

6. Inheritance 继承

So far we have always been working with the concrete (i.e. most specific type of an object). What about subclassing and interfaces?To explore this, we will differentiate between different kinds of sensors.

到目前为止,我们一直在使用具体的对象。而子类和接口又是如何使用呢? 为了探究这个问题,我们需要将不同种类的传感器区分开来。

package com.db4o.f1.chapter4;
import java.util.*;
public class SensorReadout {
  private Date time;
  private Car car;
  private String description;
  protected SensorReadout(Date time,Car car,String description) {
    this.time=time;
    this.car=car;
    this.description=description;
  }
  public Car getCar() {
    return car;
  }
  public Date getTime() {
    return time;
  }
  public String getDescription() {
    return description;
  }
  public String toString() {
    return car+" : "time" : "+description;
  }
}

 

package com.db4o.f1.chapter4;
import java.util.*;

public class TemperatureSensorReadout extends SensorReadout {
  private double temperature;

  public TemperatureSensorReadout(
  Date time,Car car,
  String description,double temperature) {
    super(time,car,description);
    this.temperature=temperature;
  }

  public double getTemperature() {
    return temperature;
  }
  public String toString() {
    return super.toString()+" temp : "+temperature;
  }
}

 

package com.db4o.f1.chapter4;
import java.util.*;

public class PressureSensorReadout extends SensorReadout {
    private double pressure;

    public PressureSensorReadout(
            Date time,Car car,
            String description,double pressure) {
        super(time,car,description);
        this.pressure=pressure;
    }

    public double getPressure() {
        return pressure;
    }

    public String toString() {
        return super.toString()+" pressure : "+pressure;
    }
}

 

Our car's snapshot mechanism is changed accordingly.

我们的赛车快照机制也作出相应改变。

package com.db4o.f1.chapter4;
import java.util.*;
public class Car {
    private String model;
    private Pilot pilot;
    private List history;
    public Car(String model) {
        this.model=model;
        this.pilot=null;
        this.history=new ArrayList();
    }
    public Pilot getPilot() {
        return pilot;
    }
    public void setPilot(Pilot pilot) {
        this.pilot=pilot;
    }
    public String getModel() {
        return model;
    }
    public SensorReadout[] getHistory() {
        return (SensorReadout[])history.toArray(new SensorReadout[history.size()]);
    }

    public void snapshot() {
        history.add(new TemperatureSensorReadout(
                new Date(),this,"oil",pollOilTemperature()));
        history.add(new TemperatureSensorReadout(
                new Date(),this,"water",pollWaterTemperature()));
        history.add(new PressureSensorReadout(
                new Date(),this,"oil",pollOilPressure()));
    }
    protected double pollOilTemperature() {
        return 0.1*history.size();
    }
    protected double pollWaterTemperature() {
        return 0.2*history.size();
    }
    protected double pollOilPressure() {
        return 0.3*history.size();
    }
    public String toString() {
        return model+"["+pilot+"]/"+history.size();
    }
}

 

6.1. Storing 存储

Our setup code has not changed at all, just the internal workings of a snapshot.

我们的设置代码一点都没有改变,因为那是快照的内部工作。

// storeFirstCar
Car car1=new Car("Ferrari");
Pilot pilot1=new Pilot("Michael Schumacher",100);
car1.setPilot(pilot1);
db.set(car1);

 

// storeSecondCar
Pilot pilot2=new Pilot("Rubens Barrichello",99);
Car car2=new Car("BMW");
car2.setPilot(pilot2);
car2.snapshot();
car2.snapshot();
db.set(car2);

6.2. Retrieving 读取

db4o will provide us with all objects of the given type. To collect all instances of a given class, no matter whether they are subclass members or direct instances, we just provide a corresponding prototype.

db4o为我们提供指定类型的所有对象。要查询指定类的所有实例,不管它们是子类成员或直接实例,我们只要提供相应的原型即可。

// retrieveTemperatureReadoutsQBE
SensorReadout proto=
new TemperatureSensorReadout(null,null,null,0.0);
ObjectSet result=db.get(proto);
listResult(result);

 

OUTPUT:
4
BMW[Rubens Barrichello/99]/6 : Tue Nov 13 03:03:03 CET 2007 : oil
temp : 0.0
BMW[Rubens Barrichello/99]/6 : Tue Nov 13 03:03:03 CET 2007 : water
temp : 0.2
BMW[Rubens Barrichello/99]/6 : Tue Nov 13 03:03:03 CET 2007 : oil
temp : 0.30000000000000004
BMW[Rubens Barrichello/99]/6 : Tue Nov 13 03:03:03 CET 2007 : water
temp : 0.8

 

// retrieveAllSensorReadoutsQBE
SensorReadout proto=new SensorReadout(null,null,null);
ObjectSet result=db.get(proto);
listResult(result);

 

OUTPUT:
6
BMW[Rubens Barrichello/99]/6 : Tue Nov 13 03:03:03 CET 2007 : oil
temp : 0.0
BMW[Rubens Barrichello/99]/6 : Tue Nov 13 03:03:03 CET 2007 : water
temp : 0.2
BMW[Rubens Barrichello/99]/6 : Tue Nov 13 03:03:03 CET 2007 : oil
pressure : 0.6
BMW[Rubens Barrichello/99]/6 : Tue Nov 13 03:03:03 CET 2007 : oil
temp : 0.30000000000000004
BMW[Rubens Barrichello/99]/6 : Tue Nov 13 03:03:03 CET 2007 : water
temp : 0.8
BMW[Rubens Barrichello/99]/6 : Tue Nov 13 03:03:03 CET 2007 : oil
pressure : 1.5

This is one more situation where QBE might not be applicable: What if the given type is an interface or an abstract class? Well, there's a little trick to keep in mind: Class objects receive special handling with QBE.

这是又一个不适用QBE的情况:当指定的类型是一个接口或抽象类时该怎么办?好,记住这个小窍门:Class对象受到了QBE的特殊处理。

// retrieveAllSensorReadoutsQBEAlternative

ObjectSet result=db.get(SensorReadout.class);

listResult(result);

 

OUTPUT:
6
BMW[Rubens Barrichello/99]/6 : Tue Nov 13 03:03:03 CET 2007 : oil
temp : 0.0
BMW[Rubens Barrichello/99]/6 : Tue Nov 13 03:03:03 CET 2007 : water
temp : 0.2
BMW[Rubens Barrichello/99]/6 : Tue Nov 13 03:03:03 CET 2007 : oil
pressure : 0.6
BMW[Rubens Barrichello/99]/6 : Tue Nov 13 03:03:03 CET 2007 : oil
temp : 0.30000000000000004
BMW[Rubens Barrichello/99]/6 : Tue Nov 13 03:03:03 CET 2007 : water
temp : 0.8
BMW[Rubens Barrichello/99]/6 : Tue Nov 13 03:03:03 CET 2007 : oil
pressure : 1.5

And of course there's our SODA API:

当然,SODA API代码如下:

// retrieveAllSensorReadoutsQuery

Query query=db.query();

query.constrain(SensorReadout.class);

ObjectSet result=query.execute();

listResult(result);

 

OUTPUT:
6
BMW[Rubens Barrichello/99]/6 : Tue Nov 13 03:03:03 CET 2007 : oil
temp : 0.0
BMW[Rubens Barrichello/99]/6 : Tue Nov 13 03:03:03 CET 2007 : water
temp : 0.2
BMW[Rubens Barrichello/99]/6 : Tue Nov 13 03:03:03 CET 2007 : oil
pressure : 0.6
BMW[Rubens Barrichello/99]/6 : Tue Nov 13 03:03:03 CET 2007 : oil
temp : 0.30000000000000004
BMW[Rubens Barrichello/99]/6 : Tue Nov 13 03:03:03 CET 2007 : water
temp : 0.8
BMW[Rubens Barrichello/99]/6 : Tue Nov 13 03:03:03 CET 2007 : oil
pressure : 1.5

6.3. Updating and deleting 更新和删除

is just the same for all objects, no matter where they are situated in the inheritance tree.Just like we retrieved all objects from the database above, we can delete all stored objects to prepare for the next chapter.

不论对象处于继承树的哪个位置,它们都是一样的。就像前面我们从数据库中获取所有对象一样,我们还要删除所有的对象来为下一章节做准备。

// deleteAll

ObjectSet result=db.get(new Object());

while(result.hasNext()) {

db.delete(result.next());

}

6.4. Conclusion 结论

Now we have covered all basic OO features and the way they are handled by db4o. We will complete the first part of our db4o walkthrough in the next chapter by looking at deep object graphs, including recursive structures.

现在我们学习了所有OO基本的特性,并且了解了db4o是如何处理它们的。我们将完成db4o第一部分,在下一章我们将深入学习对象图表,包括递归结构。

6.5. Full source 完整代码

package com.db4o.f1.chapter4;
import java.io.*;
import java.util.Arrays;
import com.db4o.*;
import com.db4o.f1.*;
import com.db4o.query.*;

public class InheritanceExample extends Util {
    public static void main(String[] args) {
        new File(Util.DB4OFILENAME).delete();
        ObjectContainer db=Db4o.openFile(Util.DB4OFILENAME);
        try {
            storeFirstCar(db);
            storeSecondCar(db);
            retrieveTemperatureReadoutsQBE(db);
            retrieveAllSensorReadoutsQBE(db);
            retrieveAllSensorReadoutsQBEAlternative(db);
            retrieveAllSensorReadoutsQuery(db);
            retrieveAllObjectsQBE(db);
        }
        finally {
            db.close();
        }
    }
    public static void storeFirstCar(ObjectContainer db) {
        Car car1=new Car("Ferrari");
        Pilot pilot1=new Pilot("Michael Schumacher",100);
        car1.setPilot(pilot1);
        db.set(car1);
    }

    public static void storeSecondCar(ObjectContainer db) {
        Pilot pilot2=new Pilot("Rubens Barrichello",99);
        Car car2=new Car("BMW");
        car2.setPilot(pilot2);
        car2.snapshot();
        car2.snapshot();
        db.set(car2);
    }
    public static void retrieveAllSensorReadoutsQBE(
            ObjectContainer db) {
        SensorReadout proto=new SensorReadout(null,null,null);
        ObjectSet result=db.get(proto);
        listResult(result);
    }
    public static void retrieveTemperatureReadoutsQBE(
            ObjectContainer db) {
        SensorReadout proto=
            new TemperatureSensorReadout(null,null,null,0.0);
        ObjectSet result=db.get(proto);
        listResult(result);
    }
    public static void retrieveAllSensorReadoutsQBEAlternative(
            ObjectContainer db) {
        ObjectSet result=db.get(SensorReadout.class);
        listResult(result);
    }
    public static void retrieveAllSensorReadoutsQuery(
            ObjectContainer db) {
        Query query=db.query();
        query.constrain(SensorReadout.class);
        ObjectSet result=query.execute();
        listResult(result);
    }

    public static void retrieveAllObjectsQBE(ObjectContainer db) {
        ObjectSet result=db.get(new Object());
        listResult(result);
    }

}

 

Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.1.3 Build:#408 Jan 23, 2006) - Bug/feature request - Contact Administrators