文档下载链接:https://download.csdn.net/download/oxygen23333/86770543
案例题目:
模拟物流快递系统程序设计(面向对象的程序设计)
学习内容:
- 掌握抽象类、抽象方法的特点
- 接口的特点
- Super关键字
- 抽象类及接口的实现
- 父类的引用指向子类的对象,多态的意义
实验思路:
- 运输货物首先需要有交通工具,所以需要定义一个交通工具类。由于交通工具可能有很多,所以可以将该交通工具类定义成一个抽象类,类中需要包含该交通工具的编号,型号以及运货负责人等属性,还需要定义一个抽象的运输方法。
- 当运输完成后,需要对交通工具进行保养,所以需要定义保养接口,具备交通工具的保养功能。
- 交通工具可能有很多种,这里可以定义一个专用运输车类,该类需要继承交通工具类,并实现保养接口。
- 在货物运输过程中,需要对运输车辆定位,以便随时跟踪货物的位置信息。定位功能可以使用GPS,而考虑到能够实现定位功能的设备可能有很多(如手机、专用定位仪器等),这时可以定义一个包含定位功能的GPS接口,以及实现了该接口的仪器类(如Phone等)。
- 编写测试类,运行查看结果。
案例编程:
工程例图:
Carmaintenance类:
package logisticspack;
//车辆保养 接口
public interface Carmaintenance {
// 保养方法
public abstract void upKeep();
}
CourierTask类:
package logisticspack;
//快递任务
public class CourierTask {
private String number;// 快递单号
private double goodsweight;
public CourierTask() {
super();
}
public CourierTask(String number, double goodsweight) {
this.number = number;
this.goodsweight = goodsweight;
}
public void sendBefore() {
System.out.println("订单开始处理,仓库验货中。。。");
System.out.println("货物重量:" + this.getGoodsweight() + "kg");
System.out.println("货物检验完毕");
System.out.println("货物填装完毕");
System.out.println("运货人已通知");
System.out.println("快递单号:" + this.getNumber());
}
public void send(Trafficandtransportation t, Phone p) {
System.out.println("运货人" + t.getAdmin() + "正在驾驶编号为" + t.getNumber() + "的" + t.getModel() + "发送货物");
t.trandport();
String showCoordinate = p.showCoordinate();
System.out.println("货物当前的坐标为:" + showCoordinate);
}
public void sendAfter(Trafficandtransportation t) {
System.out.println("货物运输任务已完成");
System.out.println("运货人" + t.getAdmin() + "所驾驶的编号为" + t.getNumber() + "的" + t.getModel() + "已归还!");
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public double getGoodsweight() {
return goodsweight;
}
public void setGoodsweight(double goodsweight) {
this.goodsweight = goodsweight;
}
}
GPS类:
package logisticspack;
//GPS 接口
public interface GPS {
// 显示坐标
public String showCoordinate();
}
Phone类:
package logisticspack;
//手机类
public class Phone {
public Phone() {
super();
}
public String showCoordinate() {
String location = "193,485";
return location;
}
}
Trafficandtransportation类:
package logisticspack;
//抽象类 交通工具 and 运行货物 方法
public abstract class Trafficandtransportation {
private String number;
private String model;
private String admin;
public Trafficandtransportation() {
// TODO Auto-generated constructor stub
super();
}
// 实现抽象类的方法
public Trafficandtransportation(String number, String model, String admin) {
this.number = number;
this.admin = admin;
this.model = model;
}
// 运输方法
public abstract void trandport();
// number
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
// model
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
// admin
public String getAdmin() {
return admin;
}
public void setAdmin(String admin) {
this.admin = admin;
}
}
ZTrafficandtransportation类:
package logisticspack;
public class ZTrafficandtransportation extends Trafficandtransportation implements Carmaintenance {
public ZTrafficandtransportation() {
super();
}
public ZTrafficandtransportation(String number, String model, String admin) {
super(number, model, admin);
}
@Override
public void upKeep() {
// TODO Auto-generated method stub
System.out.println("货物运输车辆保养完毕");
}
@Override
public void trandport() {
// TODO Auto-generated method stub
System.out.println("运输中");
}
}
Test类(测试类):
package logisticspack;
//测试类
public class Test {
public static void main(String[] args) {
CourierTask task = new CourierTask("LHB102866", 115.34);
task.sendBefore();
System.out.println("=================================");
ZTrafficandtransportation t = new ZTrafficandtransportation("Z123", "顺丰三轮车", "小李");
Phone p = new Phone();
task.send(t, p);
System.out.println("=================================");
task.sendAfter(t);
t.upKeep();
}
}