题目要求
:
1.
根据图片效果,完成题目要求
2.
编写两个类继承运输工具类:卡车类、火车类
3.
运输工具类中,添加两个变量,并进行封装。
4.
运输工具类中,添加无参构造方法和有参构造方法,并通过有参构造为重量和距离两个变量赋值
5.
运输工具类中,有计算运费的方法,方法体为空
6.
两个子类都重写父类计算运费的方法如下:
a.
卡车:运费
=
重量
x
距离
x120
,当距离大于
1000
(
km
)或者重量大于
60(t)
的时候拒载,拒载时
方法返回
-1
。
b.
火车:当距离在
900(km)
内(包含
900
)时,运费
=
重量
x
距离
X250,
大于
900
(
km
)时,运费
=
重
量
x
距离
x300
。
c.
编写测试类,创建卡车和火车类对象,调用计算运费方法
package com.zhou.com.Demo4;
public class Transp {
private double weight;
private double dist;
public Transp() {
}
public Transp(double weight, double dist) {
this.weight = weight;
this.dist = dist;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public double getDist() {
return dist;
}
public void setDist(double dist) {
this.dist = dist;
}
public void show(){
//方法体为空
}
}
package com.zhou.com.Demo4;
public class HuoChe extends Transp{
public HuoChe(double weight, double dist) {
super(weight, dist);
}
@Override
public void show() {
double freight;
if(this.getDist() <= 900){
freight = this.getWeight() * this.getDist() * 250;
}else{
freight = this.getWeight() * this.getDist() * 300;
}
System.out.println("火车的运费是:"+freight+"元");
}
}
package com.zhou.com.Demo4;
public class KaChe extends Transp{
public KaChe(double weight, double dist) {
super(weight, dist);
}
@Override
public void show() {
if(this.getDist() > 1000 || this.getWeight() >60){
System.out.println("拒载,运费为:-1");
return;
}
double freight = this.getWeight() * this.getDist() *120;
System.out.println("卡车的运费是"+freight+"元");
}
}
package com.zhou.com.Demo4;
public class Test {
public static void main(String[] args) {
KaChe kaChe = new KaChe(50,500);
HuoChe huoChe = new HuoChe(50,50);
kaChe.show();
huoChe.show();
}
}