簡體   English   中英

實現兩個對象之間的雙向全關系

[英]Implement bidirectional full relation between two objects

我有 2 個班級的母親和孩子

Class 母親:

public class Mother {

    private int id;
    private String name;
    private int age;

    public Mother(int id, String name, int age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }

Class 新生兒:

  public class NewBorn {
    
        private int id;
        private String c_daughter;
        private String s_son;
        private String name;
        private int birthdate;
        private int weight;
        private int height;
        private int motherId;
        public NewBorn(int id, String c_daughter, String s_son, String name, int birthdate, int weight, int height,int motherId) {
            this.id = id;
            this.c_daughter = c_daughter;
            this.s_son = s_son;
            this.name = name;
            this.birthdate = birthdate;
            this.weight = weight;
            this.height = height;
            this.motherId = motherId;
        }

當然,我有構造函數、setters-getters 和字符串

我覺得理解這一點並不容易。 我只需要將一個 class 擴展到另一個嗎? 像(在新生兒 class 中擴展母親?)有人可以解釋一下我如何實現兩個對象之間的雙向完整關系,所以我可以進一步說(母親可能有很多孩子,孩子只有一個母親)。

要具有完整的雙向關系,最簡單的形式應該表示如下:

Class Mother

class Mother {
  private Set<Child> children;

  // getters, setters, constructors, rest of props
}

Class Child

class Child {
  private Mother mother;

  // getters, setters, constructors, rest of props
}

您可以 在此處閱讀更多相關信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM