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

标签 java class methods public

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

类别:

/**
 * 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/

相关文章:

java - 将字母 'S' 替换为 '5'

java - java.io.File 的路径问题

java - 如何减少 GridLayout 中 JCheckboxes 之间的空间

java - 寻求固定长度的 LinkedList 变体,类似于

java - 在Java中设置另一个类中的变量

javascript - 在 JavaScript 中使用 Array 对象在数组内部的实例上调用任意方法

java - Selenium 网络驱动程序 : How to select the dynamically generated button by taking the reference of previuos tag's xpath

c++ - 错误 LNK2019,原因不明

class - 使用类引用进行多态和继承(第2部分)?

ruby - 在 Ruby 中调用 super 方法