2016-11-02 16 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()