一、类的设计
类图如下:
在这个类图中,有两个接口Careable(保养接口),GPS(定位接口);一个抽象类Transportation(运输抽象类);三个具体类:Ztransportation(专业运输车类),Phone(手机定位类),SendTask(快递发货任务类)。
其中的Ztransportation继承至Transportation类并实现Careable接口;Phone实现GPS接口;SendTask类的send()方法需要依赖Transportation类型和GPS类型的对象完成快递送货的任务。
二、编码实现
(一)、创建对应包,类文件和接口文件。项目结构如下图所示:
(二)、编写Transportation抽象类
package com.hnevc.sendtask;
public abstract class Transportation {
private String number; //编号
private String model;//型号
private String admin;//运货负责人
public Transportation(){
}
//定义构造方法
public Transportation(String number,String model,String admin){
this.number = number;
this.model = model;
this.admin = admin;
}
//定义抽象方法
abstract public void transport();
//实现对属性的封装
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getAdmin() {
return admin;
}
public void setAdmin(String admin) {
this.admin = admin;
}
}
(二)编码实现Careable接口
package com.hnevc.sendtask;
public interface Careable {
void upKeep();
(三)实现ZTransportation类
首先继承Transportation抽象类,
package com.hnevc.sendtask;
public class ZTransportation extends Transportation {
@Override
public void transport() {
// TODO Auto-generated method stub
System.out.println("运输中.......");
}
public ZTransportation(){
super();
}
//定义带参的构造方法
public ZTransportation(String number, String model,String admin){
super(number,model,admin);
}
}
然后实现Careable接口,在extends Transportation后面添加implements Careable接口,然后实现upKeep()方法。
package com.hnevc.sendtask;
public class ZTransportation extends Transportation implements Careable {
@Override
public void transport() {
// TODO Auto-generated method stub
System.out.println("运输中.......");
}
public ZTransportation(){
super();
}
//定义带参的构造方法
public ZTransportation(String number, String model,String admin){
super(number,model,admin);
}
@Override
public void upKeep() {
// TODO Auto-generated method stub
System.out.println("保养中.....");
}
}
(四)定义GPS接口,申请showCoordinate()方法。
package com.hnevc.sendtask;
public interface GPS {
String showCoordinate();
}
(五)定义Phone类,实现GPS接口
package com.hnevc.sendtask;
public class Phone implements GPS{
@Override
public String showCoordinate() {
// TODO Auto-generated method stub
String location ="1234,3434";
return location;
}
}
(六)实现SendTask类,实现send方法
package com.hnevc.sendtask;
public class SendTask {
private String number;//快递单号
private double goodsWeight;//获取重量
public SendTask(String number,double goodsWeight){
this.number = number;
this.goodsWeight = goodsWeight;
}
//发货前
public void sendBefore(){
System.out.println("订单正在处理中....");
System.out.println("货物重量"+this.goodsWeight+"KG");
System.out.println("货物验货完毕");
System.out.println("货物装货完毕");
System.out.println("通知运货人");
System.out.println("快递单号:"+this.number);
}
//发货中
public void send(Transportation t, GPS gps){
System.out.println("发货中...");
System.out.println("运货人:"+t.getAdmin());
System.out.println("货车编号:"+t.getNumber());
System.out.println("货车型号:"+t.getModel());
System.out.println("当前货车位置:"+gps.showCoordinate());
}
//发货后
public void sendAfter(Transportation t){
System.out.println("货物运输已经完成");
System.out.println("运货人:"+t.getAdmin()+"所驾驶的编号为:"+t.getNumber()+"的"+t.getModel()+"已归还");
}
}
(七)实现Test类
首先实例化一个交通工具类的对象,然后实例化一个定位对象,在实例化一个发货任务对象,调用发货任务的sendBefor,send,sendAfter等方法实现快递送货。最后调用交通工具的保养方法。
package com.hnevc.sendtask;
public class Test {
public static void main(String[] args) {
Transportation t = new ZTransportation("湘A1234567","三一货卡","张三");
GPS gps = new Phone();
SendTask sendTask = new SendTask("SP3412345345", 12.8);
sendTask.sendBefore();
sendTask.send(t, gps);
sendTask.sendAfter(t);
((ZTransportation)t).upKeep();
}
}