2014-11-22 37 views
-2

我有這樣的代碼,我可以開發它使從類Person兩個Object和他們有相同的地址(對象類) 的代碼:我有兩個階級,他們之間有一個協會,我們做一個對象數組

class Address { 
} 
class Person { 
    //Person has-a Address 

public Address[] address; 
    public Person(Address[] address) { 
    this.address = address; 
    } 
    // Other code goes here 
    } 
public Person(Address[] address) { 
    this.address = address; 
} 
// Other code goes here 
     } 

如何從類中創建數組對象?

+0

如何從類中創建數組對象? – douaa 2014-11-22 12:57:19

+0

葉蘭日Thnx :) 但你能幫助我更 – douaa 2014-11-22 13:00:14

+0

我需要兩個創建兩個人有相同的地址,並有另一種不同的地址 – douaa 2014-11-22 13:01:00

回答

0

兩個人,每一個共享的地址和一個獨特的地址:

Address[] addArr1 = new Address[2]; 
Address[] addArr2 = new Address[2]; 
addArr1[0] = new Address (...); 
addArr2[0] = addArr1[0]; // the shared Address 
addArr1[1] = new Address (...); // unique Address 
addArr2[1] = new Address (...); // unique Address 
Person p1 = new Person (addArr1); 
Person p2 = new Person (addArr2); 
0
class Address { 
} 
class Person { 
    //Person has-a Address 

    public Address[] address; 
    public Person(Address[] address) { 
     this.address = address; 
    } 
    // Other code goes here 
} 

Person[] persons = new Person[2]; 
Address[] address = new Address[100]; 
persons[0] = new Person(address); 
person[1] = new Person(address); 
+0

感謝你很多@gashu – douaa 2014-11-22 13:07:13

相關問題