我有两个类,它们之间有关联,我们创建一个对象数组

时间:2014-11-22 12:55:28

标签: java

我有这个代码,我可以开发它从类Person生成两个对象,并且它们具有相同的地址(对象类) 代码:

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
      }

如何从类创建数组对象?

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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);