Java构造器参数不匹配错误及保险类跳转问题求助
问题解决方案
1. 解决构造函数参数错误问题
你遇到的Constructor cannot be applied to given types错误,核心原因有两个:
- 自定义带参构造后,Java会自动取消默认无参构造,因此无法用
new Package()/new InsuredPackage()实例化。 - 当前带参构造完全未使用传入参数,而是直接在构造内弹出输入框,且未用
this区分类成员与局部变量,导致参数被覆盖。
修改Package类
import javax.swing.JOptionPane; public class Package { // 移除static,改为实例属性(每个包裹独立数据) String name; double weight; String shipMethod; double totalCost; String packWeight; // 临时存储输入的字符串重量 // 新增无参构造,处理用户输入逻辑 public Package() { this.name = JOptionPane.showInputDialog(null, "Enter customer name"); this.packWeight = JOptionPane.showInputDialog(null, "Package weight(in lbs)"); this.shipMethod = JOptionPane.showInputDialog(null, "Enter shipping mode(Air(A), Truck(T), or Mail(M))"); // 重量转换与异常处理 try { this.weight = Double.parseDouble(packWeight); } catch (Exception e) { System.out.println("Enter only numeric values for weight."); return; } calculateCost(); display(); } // 保留带参构造(可选,用于手动传参场景) public Package(String name, double weight, String shipMethod) { this.name = name; this.weight = weight; this.shipMethod = shipMethod; calculateCost(); display(); } // 改为实例方法,访问实例属性 public void calculateCost() { if (shipMethod.equalsIgnoreCase("A")) { totalCost = weight <=8 ? 2.00 : weight <=16 ? 3.00 :4.00; } else if (shipMethod.equalsIgnoreCase("T")) { totalCost = weight <=8 ?1.50 : weight <=16 ?2.35 :3.25; } else if (shipMethod.equalsIgnoreCase("M")) { totalCost = weight <=8 ?0.50 : weight <=16 ?1.50 :2.15; } } public void display() { System.out.println("Customer name: " + name); System.out.println("Package weight: " + weight + " lbs"); String methodName = switch(shipMethod.toUpperCase()){ case "A" -> "Air"; case "T" -> "Truck"; case "M" -> "Mail"; default -> "Unknown"; }; System.out.println("Shipping Method: " + methodName); System.out.println("Total Cost of shipping: $" + totalCost); } }
修改InsuredPackage类
public class InsuredPackage extends Package { double insurance; // 新增无参构造,调用父类无参构造 public InsuredPackage() { super(); calculateInsuranceCost(); } // 保留带参构造(可选) public InsuredPackage(String name, double weight, String shipMethod) { super(name, weight, shipMethod); calculateInsuranceCost(); } private void calculateInsuranceCost() { if (totalCost <=1) { insurance =2.45; } else if (totalCost <=3.00) { insurance =3.95; } else { insurance =5.55; } totalCost += insurance; } @Override public void display() { super.display(); System.out.println("Insurance Cost: $" + insurance); } }
2. 解决无法跳转到对应类的问题
调整UsePackage的判断逻辑,增加大小写兼容与异常输入处理:
import javax.swing.JOptionPane; public class UsePackage { public static void main(String[] args) { String ins = JOptionPane.showInputDialog(null, "Do you want insurance(yes/ no)"); if (ins == null) { // 处理用户关闭对话框的情况 System.out.println("Operation cancelled."); return; } if (ins.equalsIgnoreCase("no")) { new Package(); } else if (ins.equalsIgnoreCase("yes")) { new InsuredPackage(); } else { JOptionPane.showMessageDialog(null, "Please enter 'yes' or 'no'!"); } } }
额外优化建议
- 增加重量范围校验(禁止输入负数/0)
- 运输方式输入合法性校验(仅允许A/T/M及其大小写)
内容的提问来源于stack exchange,提问作者Cody Mangmoun
相关产品推荐
相关产品推荐

