继承的练习题

题目要求 :
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();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值