java - 如何正确增加静态变量?

标签 java methods static

这是驱动程序中的方法:

public class CustomerTest {

    private static int customerCounter = 0;

    public static boolean test1(){
        System.out.println("Test1: create a customer");
        Customer c = new Customer("Alice", "Smith");
        customerCounter++;
        return c.getName().equals("Alice Smith") && customerCounter == c.getCustomerID();
    }

    public static boolean test2() {
        System.out.println("Test2: create two customers");
        Customer c1 = new Customer("Alice", "Smith");
        Customer c2 = new Customer("Bob", "Simpson");
        customerCounter += 2;
        return c1.getName().equals("Alice Smith") && (customerCounter - 1) == c1.getCustomerID()
        && c2.getName().equals("Bob Simpson") && (customerCounter) == c2.getCustomerID();
    }

    public static void main(String[] args) {
        String result = "";
        //System.out.print("Test 1: ");
        result = test1() ? "pass." : "failed.";
        System.out.println(result);
                //System.out.print("Test 2: ");
        result = test2() ? "pass." : "failed.";
        System.out.println(result);
    }
}

这是我编写的代码:

public class Customer {

    public static final int MAX_ACCOUNTS = 5;

    private String firstName;
    private String lastName;
    private int customerID;
    private BankAccount[] accounts;
    private int numAccounts;
    private static int nextCustomerID = 1;

    //default constructor
    public Customer() {
        firstName = "";
        lastName = "";
        customerID = 1;
        accounts = null;
        numAccounts = 0;
        nextCustomerID = 1;
    }

    //Constructor sets name and initialized values
    //@param first is the first name
    //@param last is the last name
    public Customer (String first, String last)
    {
        this.firstName = first;
        this.lastName = last;
        this.customerID = 1;
        Customer.nextCustomerID = 1;

    }    

    public String getFirst(){
        return firstName;

    }
    public String getLast(){
        return lastName;
    }


    public String getName ()
    {
        return String.format("%s,%s", getFirst(),getLast());

    }
    public int getCustomerID ()
    {
        return customerID;

    }

}

当我运行驱动程序时,它返回测试 2 失败。我认为这是因为我不正确地增加了我的 nextCustomerID。

最佳答案

但是您甚至没有使用nextCustomerID

如果你要做类似的事情

public Customer (String first, String last)
{
    this.firstName = first;
    this.lastName = last;
    Customer.nextCustomerID += 1;
    this.customerID = Customer.nextCustomerID;

}   

那么我相信你的测试会起作用

编辑

将 nextCustomerID 的起始值更改为零

或更改

    this.customerID = Customer.nextCustomerID;
    Customer.nextCustomerID += 1;

关于java - 如何正确增加静态变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40373251/

相关文章:

java - 谁能帮我使用 StartingClass 中的 loadMap() 方法加载 map (文本文件)?

c++ - 冗余静态数据

java - 是否可以找到如果. Elasticsearch 文档中的单个属性被修改

java - 向 JPanel 添加组件

未执行 Java 命令

java - 如何使用 Android 从 azure 移动服务表中删除特定行?

java - 继承和多态分配

java - 如何将参数从另一个类传递给非静态方法?

c++ - h文件中的静态关键字和内部链接

c# - 为什么 Visual Studio 不警告静态字符串的循环初始化?