所以我对这个游戏还比较陌生。我有一个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);
1条答案
按热度按时间nx7onnlm1#
我知道你在学习,但是试着思考如何改进你的代码。你应该创建一些类型的类
Person
,它的属性如下parents
,name
&children
. 然后把它们加到你的Family
班级。