一、任务描述:
二、运行结果:
三、实现思路:
1、首先建立快递订单
快递任务类:变量货物重量,快递单号;
送货前方法;
送货中方法:需要车辆,定位;
送货后方法;
2、交通工具抽象类:运货人,车辆编号,车辆型号,
送货方法(抽象的,每种车的运输方法可能不同)
3、交通工具子类:实现交通工具,重写交通运输方法,
该子类需要保养;
4、保养接口:一个抽象的保养方法(在交通工具子类中重写)
5.、GPS接口:当前坐标
6 、GPS实现类phone: 具有定位功能的设备
7、测试类
四、实现代码:
用于实例化对象的父类:
私有内部变量,
添加构造方法,
添加Get , Set 方法
子类中实现构造方法
抽象类:
私有内部变量,
两个构造方法,
抽象方法,
Get方法 Set方法
子类:两个构造方法,
重写抽象方法
接口:都是抽象方法,没有方法体;
实现类:空参构造,重写方法
/**
*@Title sendTask.java
*@time 2019年6月14日 下午2:52:54
*@author
*@version 1.0
*@description 快递任务类:
*货物重量
*快递单号
*送货前方法
*送货中方法
*送货后方法
*
*
*/
package taskExpressage;
public class SendTask {
private double goodsWeight; // 货物重量
private String number; // 快递单号
// 无参构造函数
public SendTask() {
}
// 有参构造函数
public SendTask(double goodsWeight, String number) {
this.goodsWeight = goodsWeight;
this.number = number;
}
// 运送前的方法
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 sending(Transpotation tr, GPS gp) {
System.out.println("运货人 " + tr.getCarrier() + "正在驾驶编号为"
+ tr.getNumber() + "的" + tr.getModel() + "发送货物");
tr.transpotation();
System.out.println("货物当前的坐标为" + gp.showCoordinates());
}
public void sendAfter(Transpotation tr) {
System.out.println("运输任务已完成");
System.out.println("运货人" + tr.getCarrier() + "所驾驶的编号为" + tr.getNumber()
+ "的" + tr.getModel() + "已归还");
}
}
/**
*@Title Transpotation.java
*@time 2019年6月14日 下午3:42:33
*@author wangyue
*@version 1.0
*@description 交通工具抽象类
*/
package taskExpressage;
public abstract class Transpotation {
private String carrier; // 运货人
private String number; // 车辆编号
private String model; // 车辆型号
public Transpotation() {
}
public Transpotation(String carrier, String number, String model) {
this.carrier = carrier;
this.number = number;
this.model = model;
}
// 送货方法
public abstract void transpotation();
public String getCarrier() {
return carrier;
}
//GET,SET方法
public void setCarrier(String carrier) {
this.carrier = carrier;
}
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;
}
}
/**
*@Title ATranspotation.java
*@time 2019年6月14日 下午4:22:21
*@author wangyue
*@version 1.0
*@description 交通工具子类
*/
package taskExpressage;
public class ATranspotation extends Transpotation implements Careable {
// 构造方法
public ATranspotation() {
super();
}
public ATranspotation(String carrier, String number, String model) {
super(carrier, number, model);
}
@Override
public void transpotation() {
System.out.println("运输运行中。。。");
}
// 重写保养方法 implements Careable
public void upKeep() {
System.out.println("货物运输车辆保养完毕");
}
}
、
/**
*@Title Careable.java
*@time 2019年6月14日 下午4:37:54
*@author wangyue
*@version 1.0
*@description 定义保养接口
*/
package taskExpressage;
public interface Careable {
public abstract void upKeep();
}
/**
*@Title GPS.java
*@time 2019年6月14日 下午4:56:50
*@author wangyue
*@version 1.0
*@description 定义GPS接口
*
*/
package taskExpressage;
public interface GPS {
// 显示坐标的方法
public String showCoordinates();
}
/**
*@Title Phone.java
*@time 2019年6月14日 下午5:22:19
*@author wangyue
*@version 1.0
*@description GPS实现类
*/
package taskExpressage;
public class Phone implements GPS {
public Phone() {
super();
}
@Override
public String showCoordinates() {
String location = "121.323";
return location;
}
}
/**
*@Title test.java
*@time 2019年6月14日 下午5:52:12
*@author wangyue
*@version 1.0
*@description 测试类
*/
package taskExpressage;
public class test {
public static void main(String[] args) {
// 创建 快递任务对象
SendTask sendTask = new SendTask(2.34, "190617");
// 送货前
sendTask.sendBefore();
System.out.println("=============================");
// 送货中
ATranspotation tr = new ATranspotation("chenhao", "TB2572", "五菱宏光");
Phone ph = new Phone();
sendTask.sending(tr, ph);
System.out.println("=============================");
// 送货后
sendTask.sendAfter(tr);
}
}