java接口多态12

1.一个运输公司从网上得到订单,订单上标有货物重量和运输里程,编写运输接口
提示:运输方法:public double cost(double weight, double space); //weight 是重量(单位:t),space是距离(单位:km)

2.新建3个类: 卡车、火车、飞机分别实现运输接口,计算运费的方法如下:
(1).卡车:运费 = 重量 * 距离 * 120,当距离大于1000(KM)或重量大于60(t)的时候拒载,返回-1。
(2).火车:当距离在900(KM)内(包含)时,运费=重量 * 距离 * 250,大于900(KM)运费= 重量 * 距离 * 300
(3).飞机:当距离大于500(KM)时,运费=重量*距离*750,否则拒载,返回-1。

3.编写运输公司类,将运输接口作为参数,调用运输入方法

  1. 编写测试类,编写main方法,要求用户输入重量,距离,选择运输入方式,调用运输公司类中方法

就拿这个考验干部?

【以下回答由 GPT 生成】

解决方案:

首先,我们需要创建一个运输接口 Transportation,其中包含一个计算运费的方法 cost

public interface Transportation {
    double cost(double weight, double distance);
}

接下来,我们需要创建卡车、火车和飞机三个类,它们分别实现运输接口 Transportation

public class Truck implements Transportation {
    @Override
    public double cost(double weight, double distance) {
        if (distance > 1000 || weight > 60) {
            return -1;
        } else {
            return weight * distance * 120;
        }
    }
}

public class Train implements Transportation {
    @Override
    public double cost(double weight, double distance) {
        if (distance <= 900) {
            return weight * distance * 250;
        } else {
            return weight * distance * 300;
        }
    }
}

public class Airplane implements Transportation {
    @Override
    public double cost(double weight, double distance) {
        if (distance > 500) {
            return weight * distance * 750;
        } else {
            return -1;
        }
    }
}

然后,我们需要创建一个运输公司类 TransportCompany,它有一个运输接口类型的参数,并实现调用运输方法的功能。

public class TransportCompany {
    private Transportation transportation;

    public TransportCompany(Transportation transportation) {
        this.transportation = transportation;
    }

    public double calculateCost(double weight, double distance) {
        return transportation.cost(weight, distance);
    }
}

最后,我们需要创建一个测试类 Main,在 main 方法中要求用户输入重量和距离,并选择运输方式,在调用运输公司类的方法并输出结果。

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入重量(单位:吨):");
        double weight = scanner.nextDouble();
        System.out.print("请输入距离(单位:公里):");
        double distance = scanner.nextDouble();

        System.out.println("请选择运输方式:");
        System.out.println("1. 卡车");
        System.out.println("2. 火车");
        System.out.println("3. 飞机");
        int choice = scanner.nextInt();

        Transportation transportation;
        switch (choice) {
            case 1:
                transportation = new Truck();
                break;
            case 2:
                transportation = new Train();
                break;
            case 3:
                transportation = new Airplane();
                break;
            default:
                System.out.println("选择无效");
                return;
        }

        TransportCompany company = new TransportCompany(transportation);
        double cost = company.calculateCost(weight, distance);
        if (cost == -1) {
            System.out.println("拒载");
        } else {
            System.out.println("运费为:" + cost);
        }
    }
}

这样,我们就完成了以上要求。用户可以通过运输公司类调用不同的运输方式计算运费,并输出结果。如果距离或重量超出范围,则会返回拒载信息。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^