鱼C论坛

 找回密码
 立即注册
查看: 761|回复: 1

[已解决]面向对象的一道题

[复制链接]
发表于 2023-11-25 07:38:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
现需要编写一个简易快递计价程序。具体来说:

1、抽象快递类Express,其包含一个属性int weight表示快递重量(单位为kg),一个方法getWeight()用于返回快递重量和一个抽象方法getTotal()用于计算快递运费。

2、两个类继承Express,分别是:
(a)顺路快递SLExpress:计价规则为首重(1kg)12元,每增加1kg费用加2元。
(b)地地快递DDExpress:计价规则为首重(1kg)5元,每增加1kg费用加1元。

3、菜菜驿站类CaicaiStation,提供静态方法 int calculate(Express[] ex) 用于计算所有快递的费用。
  1. import java.util.Scanner;

  2. public class Main {
  3.     public static void main(String[] args) {
  4.         Scanner sc = new Scanner(System.in);
  5.         int n = sc.nextInt();
  6.         Express[] ex = new Express[n];
  7.         for (int i = 0; i < ex.length; i++) {
  8.             if (sc.next().equals("SL"))
  9.                 ex[i] = new SLExpress(sc.nextInt());
  10.             else
  11.                 ex[i] = new DDExpress(sc.nextInt());
  12.         }

  13.         System.out.println(CaicaiStation.calculate(ex));
  14.         sc.close();
  15.     }
  16. }
  17. /* 请在这里填写答案 */
复制代码
最佳答案
2023-11-25 07:38:50
在菜菜驿站类CaicaiStation中,我们需要提供一个静态方法int calculate(Express ex),用于计算所有快递的费用。可以按照以下步骤来实现:

1. 在Express类中,添加一个抽象方法getTotal(),用于计算快递运费。同时添加一个getWeight()方法,用于返回快递重量。
  1. public abstract class Express {
  2.     private int weight;
  3.     public Express(int weight) {
  4.         this.weight = weight;
  5.     }
  6.     public int getWeight() {
  7.         return weight;
  8.     }
  9.     public abstract int getTotal();
  10. }
复制代码

2. 创建两个子类:SLExpress和DDExpress,分别继承Express类,并实现getTotal()方法。
  1. public class SLExpress extends Express {
  2.     public SLExpress(int weight) {
  3.         super(weight);
  4.     }
  5.     @Override
  6.     public int getTotal() {
  7.         int weight = getWeight();
  8.         int basePrice = 12; // 首重费用
  9.         int additionalPrice = 2; // 每增加1kg的费用
  10.         return basePrice + additionalPrice * (weight - 1);
  11.     }
  12. }
  13. public class DDExpress extends Express {
  14.     public DDExpress(int weight) {
  15.         super(weight);
  16.     }
  17.     @Override
  18.     public int getTotal() {
  19.         int weight = getWeight();
  20.         int basePrice = 5; // 首重费用
  21.         int additionalPrice = 1; // 每增加1kg的费用
  22.         return basePrice + additionalPrice * (weight - 1);
  23.     }
  24. }
复制代码

3. 在CaicaiStation类中,实现静态方法calculate(Express ex),用于计算所有快递的费用。
  1. public class CaicaiStation {
  2.     public static int calculate(Express ex) {
  3.         return ex.getTotal();
  4.     }
  5. }
复制代码

4. 在Main类中,根据用户输入的快递信息,创建对应的SLExpress或DDExpress对象,并调用CaicaiStation.calculate()方法计算费用。
  1. import java.util.Scanner;
  2. public class Main {
  3.     public static void main(String[] args) {
  4.         Scanner sc = new Scanner(System.in);
  5.         int n = sc.nextInt();
  6.         Express[] ex = new Express[n];
  7.         
  8.         for (int i = 0; i < n; i++) {
  9.             if (sc.next().equals("SL")) {
  10.                 int weight = sc.nextInt();
  11.                 ex[i] = new SLExpress(weight);
  12.             } else {
  13.                 int weight = sc.nextInt();
  14.                 ex[i] = new DDExpress(weight);
  15.             }
  16.         }
  17.         for (int i = 0; i < n; i++) {
  18.             System.out.println(CaicaiStation.calculate(ex[i]));
  19.         }
  20.         sc.close();
  21.     }
  22. }
复制代码

这样,就可以根据用户输入的快递信息计算出对应的运费了。
以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-11-25 07:38:50 | 显示全部楼层    本楼为最佳答案   
在菜菜驿站类CaicaiStation中,我们需要提供一个静态方法int calculate(Express ex),用于计算所有快递的费用。可以按照以下步骤来实现:

1. 在Express类中,添加一个抽象方法getTotal(),用于计算快递运费。同时添加一个getWeight()方法,用于返回快递重量。
  1. public abstract class Express {
  2.     private int weight;
  3.     public Express(int weight) {
  4.         this.weight = weight;
  5.     }
  6.     public int getWeight() {
  7.         return weight;
  8.     }
  9.     public abstract int getTotal();
  10. }
复制代码

2. 创建两个子类:SLExpress和DDExpress,分别继承Express类,并实现getTotal()方法。
  1. public class SLExpress extends Express {
  2.     public SLExpress(int weight) {
  3.         super(weight);
  4.     }
  5.     @Override
  6.     public int getTotal() {
  7.         int weight = getWeight();
  8.         int basePrice = 12; // 首重费用
  9.         int additionalPrice = 2; // 每增加1kg的费用
  10.         return basePrice + additionalPrice * (weight - 1);
  11.     }
  12. }
  13. public class DDExpress extends Express {
  14.     public DDExpress(int weight) {
  15.         super(weight);
  16.     }
  17.     @Override
  18.     public int getTotal() {
  19.         int weight = getWeight();
  20.         int basePrice = 5; // 首重费用
  21.         int additionalPrice = 1; // 每增加1kg的费用
  22.         return basePrice + additionalPrice * (weight - 1);
  23.     }
  24. }
复制代码

3. 在CaicaiStation类中,实现静态方法calculate(Express ex),用于计算所有快递的费用。
  1. public class CaicaiStation {
  2.     public static int calculate(Express ex) {
  3.         return ex.getTotal();
  4.     }
  5. }
复制代码

4. 在Main类中,根据用户输入的快递信息,创建对应的SLExpress或DDExpress对象,并调用CaicaiStation.calculate()方法计算费用。
  1. import java.util.Scanner;
  2. public class Main {
  3.     public static void main(String[] args) {
  4.         Scanner sc = new Scanner(System.in);
  5.         int n = sc.nextInt();
  6.         Express[] ex = new Express[n];
  7.         
  8.         for (int i = 0; i < n; i++) {
  9.             if (sc.next().equals("SL")) {
  10.                 int weight = sc.nextInt();
  11.                 ex[i] = new SLExpress(weight);
  12.             } else {
  13.                 int weight = sc.nextInt();
  14.                 ex[i] = new DDExpress(weight);
  15.             }
  16.         }
  17.         for (int i = 0; i < n; i++) {
  18.             System.out.println(CaicaiStation.calculate(ex[i]));
  19.         }
  20.         sc.close();
  21.     }
  22. }
复制代码

这样,就可以根据用户输入的快递信息计算出对应的运费了。
以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 19:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表