Java数据对象(JDO)介绍(三)
|
|
|
作者:爱心 来源:爱心 加入时间:2006年01月26日 |
|
Java数据对象(JDO)介绍(三) 站点:爱心种子小博士 关键字:Java数据对象(JDO)介绍(三)
|
Java 数据对象(JDO)介绍(三) Java 数据对象(JDO)介绍(三) 2003-7-3 Matrix让你不断进步 浏览选项: 颜色 默认 灰度 橄榄色 绿色 蓝色 褐色 红色 本文已被浏览 14 次 试例 简介 下面的例子示范了JDO的基础应用。代码示范了如何向数据库添加数据和取回数据。所有的代码使用的都是SolarMetric´s Kodo JDO实现。包括创建数据库的机制,使域对象类扩展PersistenceCapable接口的增强和厂商特定的PersistenceManagerFactory实现使用的都是Kodo JDO实现。其他的部分使用的是标准的JDO实现。
域对象 The samples will work with a small set of classes, which represent a Fleet of Vehicle objects. The two specific types of Vehicles defined are Bicycle and MotorVehicle. MotorVehicle objects have an Engine attribute. 例子是用了一个很小的类,这个类描述了一个Vehicle(交通工具) 对象的Fleet(车队)。Vehicle 定义了两个特定的对象Bicycle 和MotorVehicle 。MotorVehicle对象有一个Engine 属性。 域对象源代码 /** * Fleet.java */ package com.ociweb.jdodemo;
import java.util.Iterator; import java.util.List; import java.util.Vector;
public class Fleet {
private List vehicles = new Vector();
public void addVehicle(Vehicle vehicle) { vehicles.add(vehicle); }
public Iterator getVehicles() { return vehicles.iterator(); }
public String toString() { StringBuffer buffer = new StringBuffer("Fleet:
"); Iterator iter = getVehicles(); while (iter.hasNext()) { buffer.append(" " + iter.next() + "
"); } return buffer.toString(); } }
/** * Vehicle.java */ package com.ociweb.jdodemo;
public class Vehicle {
private int numberOfWheels;
public Vehicle(int numberOfWheels) { this.numberOfWheels = numberOfWheels; }
public int getNumberOfWheels() { return numberOfWheels; } }
/** * Bicycle.java */ package com.ociweb.jdodemo;
public class Bicycle extends Vehicle {
private String model;
public Bicycle(String model) { super(2); this.model = model; }
public String toString() { return "Bike: Model " + model; } }
/** * MotorVehicle.java */ package com.ociweb.jdodemo;
public class MotorVehicle extends Vehicle {
private Engine engine;
public MotorVehicle(int numberOfWheels, Engine engine) { super(numberOfWheels); this.engine = engine; }
public String toString() { return "MotorVehicle With " + getNumberOfWheels() + " Wheels. " + engine; } }
/** * Engine.java */ package com.ociweb.jdodemo;
public class Engine {
private int numberOfCylinders;
public Engine(int numberOfCylinders) { this.numberOfCylinders = numberOfCylinders; }
public int getNumberOfCylinders() { return numberOfCylinders; }
public String toString() { return numberOfCylinders + " Cylinder Engine."; } } |
|
|
|
|