阳哥,代码有个Bug,祖父母的父母为null,但打印对象时没有做null判断处理,会抛出空指针异常。偷懒没注意了。。。
//grandfatherOfFather, grandmotherOfFather分别作为小红她爸的爸爸和妈妈
Person grandfatherOfFather = new Person("Tom", Person.MALE, null, null);
Person grandmotherOfFather = new Person("Marry", Person.FEMALE, null, null);
//grandfatherOfFather, grandmotherOfFather分别作为小红她爸妈的爸爸和妈妈
Person grandfatherOfMather = new Person("Alex",Person.MALE, null, null);
Person grandmotherOfMather = new Person("Grace",Person.FEMALE, null, null);
---------------------------------------------------------------------------
@Override
public String toString() {
String mGender = gender == Person.MALE ? "male" : "female";
StringBuffer sb = new StringBuffer();
sb.append("My name is " + name + " ,")
.append("I am a " + mGender + " , ")
.append("my father is " + getFather().getName() + " , ")
.append("my mother is " + getMother().getName() + " . ");
return sb.toString();
}
|