я понимаю что у меня ошибка в методе ToString,но почему он у меня путает мать с отцом? ведь я ему явно указал кто есть кто???
package com.javarush.task.task07.task0724;
/*
Семейная перепись
*/
public class Solution {
public static void main(String[] args) {
Human anna = new Human("anna",false,70);
Human inna = new Human("inna",false,70);
Human igor = new Human("igor",true,75);
Human ivan = new Human("ivan",true,75);
Human sasha = new Human("sasha",true,30,anna,igor);
Human inna2 = new Human("inna2",false,30,inna,ivan);
Human sveta = new Human("sveta",false,18,sasha,inna2);
Human stepa = new Human("stepa",true,18,sasha,inna2);
Human oleg = new Human("oleg",true,18,sasha,inna2);
String semya = anna.toString()+inna.toString()+igor.toString()+
ivan.toString()+sasha.toString()+inna2.toString()+
sveta.toString()+stepa.toString()+oleg.toString();
System.out.println(semya);
}
public static class Human {
String name;
boolean sex;
int age;
Human mother;
Human father;
public Human(String name,boolean sex,int age){
this.name = name;
this.sex = sex;
this.age = age;
}
public Human(String name,boolean sex,int age,Human father,Human mother){
this.name = name;
this.sex = sex;
this.age = age;
this.father = father;
this.mother = mother;
}
public String toString() {
String text = "";
text += "Имя: " + this.name;
text += ", пол: " + (this.sex ? "мужской" : "женский");
text += ", возраст: " + this.age;
if(mother !=null && father !=null){
text+=", отец:"+ this.mother.name +", мать:"+this.father.name;
}
return text;
}
}
}