2016-11-02 29 views
1

我有一個驅動程序,充當我的測試人員。如何從驅動程序訪問複製構造函數

這裏是驅動程序:

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 boolean test4() { 
     System.out.println("Test4: copy a customer"); 
     Customer c1 = new Customer("Alice", "Smith"); 
     Customer c2 = new Customer("Bob", "Simpson"); 
     c1.copy(c2); 
     customerCounter += 2; 
     return c1.getName().equals("Bob Simpson") && (customerCounter) == c1.getCustomerID() 
      && c2.getName().equals("Bob Simpson") && (customerCounter) == c2.getCustomerID() 
      && c1 != c2; 
    } 
    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); 

     //System.out.print("Test 4: "); 
     result = test4() ? "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 = nextCustomerID; 
     accounts = null; 
     numAccounts = 0; 
     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; 
     this.customerID = nextCustomerID; 
     nextCustomerID++; 



    }  

    public void copy (Customer copyFrom) 
    { 
     Customer aCustomer = new Customer(); 
     aCustomer.firstName = copyFrom.firstName; 
     aCustomer.lastName = copyFrom.lastName; 
     aCustomer.customerID = copyFrom.customerID; 
     aCustomer.accounts = copyFrom.accounts; 
     aCustomer.numAccounts = copyFrom.numAccounts; 
    } 
} 

我的拷貝構造函數失敗的驅動程序測試4.我不知道爲什麼,因爲我複製在該方法中調用的所有內容。

+0

TEST4檢查if(customerCounter)== c1.getCustomerID()和(customerCounter)== c2.getCustomerID()。這真的是應該是什麼?這意味着兩個客戶都有相同的ID,這聽起來並不合適。 – Aziuth

回答

3

正如我所見copy()不是你的情況下的構造函數它只是一個以前創建的對象的方法。如果你要創建的對象,然後從另一個對象填充它,你需要寫的東西是這樣的:

public void copy (Customer copyFrom) { 
     this.firstName = copyFrom.firstName; 
     this.lastName = copyFrom.lastName; 
     this.customerID = copyFrom.customerID; 
     this.accounts = copyFrom.accounts; 
     this.numAccounts = copyFrom.numAccounts; 
    } 
1

你的方法複製(客戶的copyfrom)不是一個構造函數。構造函數返回一個新的客戶對象。你的副本做的是創建一個空的客戶,分配字段值,就是這樣。這個新客戶在方法運行結束時從內存中消失。您可以通過複製解決它()返回新的客戶,如:

public Customer copy(Customer copyFrom)... 

或者寫一個真正的拷貝構造函數:

public Customer(Customer copyFrom) 
    { 
     this.firstName = copyFrom.firstName; 
     this.lastName = copyFrom.lastName; 
     this.customerID = copyFrom.customerID; 
     this.accounts = copyFrom.accounts; 
     this.numAccounts = copyFrom.numAccounts; 
    } 
0

在該方法中複製(這不是一個構造函數)你不該」 t使用

Customer aCustomer = new Customer(); 

因爲您不想創建客戶的新實例,您想修改當前的實例。你應該在當前實例(this)的每個屬性分配給背景copyFrom值:

public void copy (Customer copyFrom) 
    { 
     this.firstName = copyFrom.firstName; 
     this.lastName = copyFrom.lastName; 
     this.customerID = copyFrom.customerID; 
     this.accounts = copyFrom.accounts; 
     this.numAccounts = copyFrom.numAccounts; 
    } 

你現在正在做它的方式,你也可以決定返回aCustomer如果你想創建一個新的副本,但這將是一個奇怪的方法,如果你把複製方法Customer類裏面,倒不如把它在其他類(我把它叫做CustomerHelper)作爲一個靜態方法:

public static Customer copy (Customer copyFrom) 
{ 
    Customer aCustomer = new Customer(); 
    aCustomer.firstName = copyFrom.firstName; 
    aCustomer.lastName = copyFrom.lastName; 
    aCustomer.customerID = copyFrom.customerID; 
    aCustomer.accounts = copyFrom.accounts; 
    aCustomer.numAccounts = copyFrom.numAccounts; 
    return aCustomer; 
} 

,然後:

c1 = CustomerHelper.copy(c2); 

或者,如果你想創建一個新的實例,它是當前實例的副本,你可以使用這樣的方法:

public Customer copy() 
    { 
    Customer aCustomer = new Customer(); 
    aCustomer.firstName = this.firstName; 
    aCustomer.lastName = this.lastName; 
    aCustomer.customerID = this.customerID; 
    aCustomer.accounts = this.accounts; 
    aCustomer.numAccounts = this.numAccounts; 
    return aCustomer; 
    } 

,並使用它:

c1=c2.copy()