Java 如何从驱动程序访问复制构造函数

Java 如何从驱动程序访问复制构造函数,java,methods,copy-constructor,deep-copy,Java,Methods,Copy Constructor,Deep Copy,我有一个司机作为我的测试人员 司机来了: 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"); cus

我有一个司机作为我的测试人员

司机来了:

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。我不知道为什么,因为我复制了方法中调用的所有内容

如我所见,
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;
    }
正如我所看到的,
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;
    }
您的方法副本(Customer copyFrom)不是构造函数。构造函数返回一个新的客户对象。您的副本所做的是创建一个空客户,分配字段值,就是这样。这个新客户在方法运行结束时从内存中消失。您可以通过copy()返回新客户来解决此问题,如:

    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;
    }
您的方法副本(Customer copyFrom)不是构造函数。构造函数返回一个新的客户对象。您的副本所做的是创建一个空客户,分配字段值,就是这样。这个新客户在方法运行结束时从内存中消失。您可以通过copy()返回新客户来解决此问题,如:

    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;
    }
在方法copy(不是构造函数)中,您不应该使用

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
,但如果将copy方法放在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()
在方法copy(不是构造函数)中,您不应该使用

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
,但如果将copy方法放在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()
测试4检查(customerCounter)==c1.getCustomerID()和(customerCounter)==c2.getCustomerID()。这是真的吗?这意味着两个客户都有相同的id,这对我来说并不正确。test4检查(customerCounter)==c1.getCustomerID()和(customerCounter)==c2.getCustomerID()。这是真的吗?这意味着两个客户都有相同的id,这对我来说并不合适。