如何正确增加静态变量?

时间:2016-11-02 05:48:52

标签: 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。

3 个答案:

答案 0 :(得分:1)

但你甚至没有使用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;

答案 1 :(得分:0)

你永远不会增加nextCustomerId。它是一个私有变量,因此它只能在Customer内更改,并且您引用它的唯一位置是为其赋值1.

构造函数的以下两行不正确:

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

将其替换为:

this.customerID = nextCustomerId;
nextCustomerID++;

(您还应该以类似的方式更新默认构造函数)。

++运算符将变量的值增加1

=运算符是一个简单的赋值运算符,只需将左侧的变量设置为右侧的值即可。

值得回顾Java Introduction to Assignment and Unary operators

答案 2 :(得分:0)

问题:

1)nextCustomerID没有递增

2)名称不匹配,因为你的getName函数有一个逗号。

所以:

客户(构造函数):

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

    //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;
        Customer.nextCustomerID += 1;
        this.customerID = Customer.nextCustomerID;

    }    

CustomerTest:

public class CustomerTest {

    private static int customerCounter = 1;

    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;
        System.out.println ( c1.getName ( ) +" " + c2.getName ( ) );
        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);
    }
}