【问题标题】:Class created and Public Methods not going over to Test Method创建的类和公共方法不转到测试方法
【发布时间】:2015-02-07 16:24:28
【问题描述】:

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

类:

/**
 * 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");

    }

}

【问题讨论】:

  • 您正在声明一个绝对不包含任何成员的收银机类,它声明了另一个包含一些成员的嵌套收银机类。然后,您正在尝试测试外部类。当然它不会编译,因为外部类没有成员。

标签: java class methods public


【解决方案1】:

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

【讨论】:

  • 没问题!我们随时为您提供帮助。
【解决方案2】:

你有课内课:

 public class cashRegister { /** * A simulated cash register that tracks the item count and 3 the total * amount due. */ 
     public class CashRegister

只需删除 public class CashRegister 即可。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-17
    • 1970-01-01
    • 2010-10-01
    • 2023-03-19
    • 1970-01-01
    • 2011-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多