如何将这两个类连接起来,以展示Person和Address之间的一对多关系。

huangapple go评论74阅读模式
英文:

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&lt;Address&gt; 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 userIds 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.

huangapple
  • 本文由 发表于 2020年10月7日 00:59:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64230489.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定