英文:
How would I link the two classes together to show the one to many relationship between Person and Address
问题
Address类:
public class Address {
private String country;
private String county;
private String city;
private String postcode;
private String HouseNumber;
public Address(String country, String county, String city, String postcode, String HouseNumber) {
this.country = country;
this.county = county;
this.city = city;
this.postcode = postcode;
this.HouseNumber = HouseNumber;
}
public void view_adress() {
String[] address = {country, county, city, postcode, HouseNumber};
for (int i = 0; i<address.length; i++) {
System.out.println(address[i]);
}
}
public void viewHouseNumber() {
System.out.print(HouseNumber);
}
}
Person类:
public class Person {
private String firstName;
private String lastName;
private String Date_of_birth;
private String PhoneNumber;
private String[] address;
public Person (String firstName, String lastName, String Date_of_birth, String PhoneNumber, String[] address) {
this.firstName = firstName;
this.lastName = lastName;
this.Date_of_birth = Date_of_birth;
this.PhoneNumber = PhoneNumber;
this.address = address;
}
public void view_PhoneNumber() {
System.out.print(PhoneNumber);
}
}
英文:
Address class:
public class Address {
private String country;
private String county;
private String city;
private String postcode;
private String HouseNumber;
public Address(String country, String county, String city, String postcode, String HouseNumber) {
this.country = country;
this.county = county;
this.city = city;
this.postcode = postcode;
this.HouseNumber = HouseNumber;
}
public void view_adress() {
String[] address = {country, county, city, postcode, HouseNumber};
for (int i = 0; i<address.length; i++) {
System.out.println(address[i]);
}
}
public void viewHouseNumber() {
System.out.print(HouseNumber);
}
}
Person class:
public class Person {
private String firstName;
private String lastName;
private String Date_of_birth;
private String PhoneNumber;
private String[] address;
public Person (String firstName, String lastName, String Date_of_birth, String PhoneNumber, String[] address) {
this.firstName = firstName;
this.lastName = lastName;
this.Date_of_birth = Date_of_birth;
this.PhoneNumber = PhoneNumber;
this.address = address;
}
public void view_PhoneNumber() {
System.out.print(PhoneNumber);
}
}
答案1
得分: 2
利用面向对象编程 Composition。
public class Person {
//...
List<Address> addresses;
//...
}
Person
的一个实例可以拥有 0 或多个 Address
的实例。
请注意,在实际情况中,最好在你的 Address
类中保留一个 userId
的列表,因为 不止一个 用户可能有一个或多个地址,这意味着你的关系必须是 Many-To-Many
。
同样重要的一点是遵守 Java 命名约定,命名方式如下:
- 类使用
PascalCase
; - 字段和方法名称使用
camelCase
; - 常量使用
ALL_CAPS_SEPARATED_WITH_UNDERSCORES
。
英文:
Make use of OOP Composition.
public class Person {
//...
List<Address> addresses;
//...
}
One instance of a Person
will have a 0 or more instances of Address
.
Note, that in a real world scenario, you better want to retain a list of userId
s in your Address
class as well, because, more-than-one users, might have one, or also more-than-one addresses, which means, that that your relation must be Many-To-Many
.
No less (at all) important thing it to stick with the Java Naming Conventions and name:
- classes with
PascalCase
; - fields and method names with
camelCase
; - constants with
ALL_CAPS_SEPARATED_WITH_UNDERSCORES
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论