如何证明两个孩子是兄弟姐妹?

cgyqldqp  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(385)

所以我对这个游戏还比较陌生。我有一个java类“family”,我想在其中实现一个boolean方法,告诉我x是y的兄弟(true还是false)等等。

public class Family {

  private final Gender gender; // enum
  private final String name;
  private Family parent;
  private Family firstChild;
  private Family seccondChild;
  private Family thirdChild;

  public Family (final String name, final Gender gender) {
    this.name = name;
    this.gender = gender;
  }

  public String getName() {
    return name;
  }

  public Gender getGender() {
    return gender;
  }

  public Family getThirdChild() {
    return thirdChild;
  }

  public void setThirdChild(Family thirdChild) {
    this.thirdChild = thirdChild;
  }

  public void setSeccondChild(Family seccondChild) {
    this.seccondChild = seccondChild;
  }

  public Family getSeccondChild() {
    return seccondChild;
  }

  public Family getFirstChild() {
    return firstChild;
  }

  public void setFirstChild(Family firstChild) {
    this.firstChild = firstChild;
  }

  public Family getParent() {
    return parent;
  }

  public void setParent(Family parent) {
    this.parent = parent;
  }

  // So here is my problem I don't know how to show, that two children are siblings or not

public boolean isTheSiblingOf(Family x) {
    if ( ) {    // If  y is sibling of x return true.. How?? 
      return true;
    }
    return false;
  }

  public String toString() {
    return "Family: " + name;
  }

}

这是我的对象的另一个类。如你所见,我只提到母亲。我需要父亲做别的事,但不是现在。

Family theo = new Family("Theo", Gender.M); // Father
Family clara = new Family ("Clara", Gender.F); // Mother

Family john = new Family("John", Gender.M);
john.setParent(clara); // I'm only choosing one parent
clara.setFirstChild(john);

Family rachel = new Family("Rachel", Gender.F);
rachel.setParent(clara);
clara.setSeccondChild(rachel);

Family jennifer = new Family("Jennifer", Gender.F);
jennifer.setParent(clara);
clara.setThirdChild(jennifer);
nx7onnlm

nx7onnlm1#

我知道你在学习,但是试着思考如何改进你的代码。你应该创建一些类型的类 Person ,它的属性如下 parents , name & children . 然后把它们加到你的 Family 班级。

public class Person {

    private Person mother;
    private Person father;       
    private String firstName;
    private String lastName;
    private List<Person> children = new ArrayList<Person>();

    public Person(Person mother, Person father, String firstName, String lastName) {
        this.mother = mother;
        this.father = father;
        this.firstame = firstName;
        this.lastName = lastName;
    }

    void addChildren(Person child) {
        children.add(child);
    }

    // GETERS AND SETTERS
}

public class Family {

    private String familyName;
    private List<Person> familyMembers = new ArrayList<Person>();

    public Family(String name) {
        this.familyName = name;
    }

    public boolean areSiblings(Person person1, Person person2) {
        if(person1.getMother().equals(person2.getMother())
            && person1.getFather().equals(person2.getFather())) {
            return true;
        } else {
            return false;
        }
    }
}

相关问题