gpt4 book ai didi

java - 创建的类和公共(public)方法未转到测试方法

转载 作者:行者123 更新时间:2023-12-01 11:56:54 25 4
gpt4 key购买 nike

我创建了一个非常简单的收银机类,并想用单独的方法对其进行测试。当我从测试方法调用类中创建的方法时,会出现错误,指出某个方法在创建它的类中未定义,但事实并非如此。有人可以解释一下为什么我会收到此错误吗?谢谢。

类别:

/**
* A simulated cash register that tracks the item count and 3 the total
* amount due.
*/
public class CashRegister {
private int itemCount;
private double totalPrice;


public CashRegister() {
itemCount = 0;
totalPrice = 0;
}


public void addItem(double price) {
itemCount++;
totalPrice = totalPrice + price;
}

public double getTotal() {
return totalPrice;
}


public int getCount() {
return itemCount;
}

public void clear() {
itemCount = 0;
totalPrice = 0;
}
}

测试类:

public class cashRegisterTester {

public static void main(String[] args) {

cashRegister register1 = new cashRegister();

register1.addItem(0.95);
register1.addItem(2.50);
System.out.println(register1.getCount());
System.out.println("Expected: 3");
System.out.printf("%.2f\n", register1.getTotal());
System.out.println("Expected: 5.40");

}

}

最佳答案

去掉第一个 public class cashRegister { 行和最后一个 }

关于java - 创建的类和公共(public)方法未转到测试方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28384463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com