2016-11-02 8 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); 

ここで私がこれまでに書いたコードです:私のコピーコンストラクタは、ドライバ・テスト4を失敗した

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

私はコピーなぜのでわかりませんが、メソッドで呼び出されるすべて

+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

(コンストラクタではありません)あなたは「shouldnあなたは次のように新規顧客を返す)(コピーによってそれを解決することができます

Customer aCustomer = new Customer(); 

お客様の新しいインスタンスを作成する必要がないため、現在のインスタンスを変更する必要があるためです。あなたは、インスタンスcopyFromの値に現在のインスタンス(this)のすべての属性を割り当てる必要があります。

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() 
関連する問題