Java技术问询:如何通过构造函数为方法传入参数?
如何通过构造函数为方法传入参数(结合你的Invoice类)
先把你提供的Invoice类代码整理出来:
public class Invoice { ArrayList<Double> ItemPrices= new ArrayList<Double>(); ArrayList<Boolean> ItemIsPet = new ArrayList<Boolean>(); ArrayList<Integer> ItemQuantitys = new ArrayList<Integer>(); public void add(Item anItem){ } public static void main(String args[]) { int counter = 0; boolean pet; Scanner s = new Scanner(System.in); do { System.out.println(); System.out.print("Item " + counter+1 + ":"); System.out.println(); // 未完成的输入逻辑 } while(...); // 假设存在循环终止条件 } }
结合你的代码场景,我分两种常见需求给你讲清楚构造函数传参的用法:
1. 给Item类加构造函数,方便创建对象传入Invoice的add方法
你的add方法接收Item类型参数,但目前没有完整的Item类定义。我们可以给Item添加构造函数,让创建对象时直接传入属性参数,再在Invoice的add方法中提取这些参数存入列表。
先定义带构造函数的Item类:
public class Item { private double price; private boolean isPet; private int quantity; // Item的构造函数,初始化所有属性 public Item(double price, boolean isPet, int quantity) { this.price = price; this.isPet = isPet; this.quantity = quantity; } // 提供getter方法,让Invoice能读取Item的属性 public double getPrice() { return price; } public boolean isPet() { return isPet; } public int getQuantity() { return quantity; } }
然后修改Invoice的add方法,利用构造函数传入的参数:
public class Invoice { ArrayList<Double> ItemPrices= new ArrayList<Double>(); ArrayList<Boolean> ItemIsPet = new ArrayList<Boolean>(); ArrayList<Integer> ItemQuantitys = new ArrayList<Integer>(); public void add(Item anItem){ // 从Item对象中取出构造函数传入的参数,存入对应列表 ItemPrices.add(anItem.getPrice()); ItemIsPet.add(anItem.isPet()); ItemQuantitys.add(anItem.getQuantity()); } public static void main(String args[]) { int counter = 0; Scanner s = new Scanner(System.in); Invoice invoice = new Invoice(); do { System.out.println(); System.out.print("Item " + (counter+1) + ":"); System.out.println(); // 模拟控制台输入获取参数 System.out.print("输入商品价格: "); double price = s.nextDouble(); System.out.print("是否为宠物用品? (true/false): "); boolean isPet = s.nextBoolean(); System.out.print("输入数量: "); int quantity = s.nextInt(); // 通过构造函数直接传入参数创建Item对象 Item newItem = new Item(price, isPet, quantity); // 将Item对象传入add方法 invoice.add(newItem); counter++; System.out.print("继续添加商品吗? (y/n): "); String choice = s.next(); } while(choice.equalsIgnoreCase("y")); } }
2. 给Invoice类加构造函数,传入全局参数供方法使用
如果你的Invoice需要一些全局参数(比如税率、折扣率),这些参数会被多个方法复用,就可以通过构造函数传入并保存为成员变量,后续方法直接调用即可。
比如给Invoice添加构造函数传入税率:
public class Invoice { ArrayList<Double> ItemPrices= new ArrayList<Double>(); ArrayList<Boolean> ItemIsPet = new ArrayList<Boolean>(); ArrayList<Integer> ItemQuantitys = new ArrayList<Integer>(); private double taxRate; // 新增税率成员变量 // Invoice的构造函数,传入税率参数 public Invoice(double taxRate) { this.taxRate = taxRate; } public void add(Item anItem){ ItemPrices.add(anItem.getPrice()); ItemIsPet.add(anItem.isPet()); ItemQuantitys.add(anItem.getQuantity()); } // 使用构造函数传入的税率计算含税总价 public double calculateTotal() { double subTotal = 0; for(int i=0; i<ItemPrices.size(); i++){ subTotal += ItemPrices.get(i) * ItemQuantitys.get(i); } return subTotal * (1 + taxRate); } public static void main(String args[]) { // 创建Invoice时通过构造函数传入8%的税率 Invoice invoice = new Invoice(0.08); int counter = 0; Scanner s = new Scanner(System.in); do { // 输入逻辑和之前一致 System.out.println(); System.out.print("Item " + (counter+1) + ":"); System.out.println(); System.out.print("输入商品价格: "); double price = s.nextDouble(); System.out.print("是否为宠物用品? (true/false): "); boolean isPet = s.nextBoolean(); System.out.print("输入数量: "); int quantity = s.nextInt(); Item newItem = new Item(price, isPet, quantity); invoice.add(newItem); counter++; System.out.print("继续添加商品吗? (y/n): "); String choice = s.next(); } while(choice.equalsIgnoreCase("y")); // 调用计算方法,直接使用构造函数传入的税率 System.out.println("含税总价: " + invoice.calculateTotal()); } }
核心思路总结
- 构造函数的本质是初始化对象状态:把需要的参数传入后赋值给成员变量,类内的方法就能直接使用这些变量。
- 给其他类传参:给目标类(比如Item)加构造函数,创建对象时传入参数,再把对象传给目标方法。
- 给当前类方法传全局参数:给当前类(比如Invoice)加构造函数,把参数存为成员变量,方法直接调用即可。
内容的提问来源于stack exchange,提问作者NULLPTR
相关产品推荐
相关产品推荐

