gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 08:40:48 25 4
gpt4 key购买 nike

我有一个司机充当我的测试员。

这是驱动程序:

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;
}

关于java - 如何从驱动程序访问复制构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40375119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com