Прошел валидацию с таким кодом, подскажите, пожалуйста, как я умудрился вызвать метод toString ? потому как в выводе у меня получается
Имя: 1, пол: мужской, возраст: 1
Имя: 2, пол: мужской, возраст: 2
Имя: 3, пол: женский, возраст: 3
Имя: 4, пол: женский, возраст: 4
Имя: 5, пол: мужской, возраст: 5, отец: 1, мать: 2
Имя: 5, пол: мужской, возраст: 5, отец: 1, мать: 2
Имя: 5, пол: мужской, возраст: 5, отец: 1, мать: 2
Имя: 5, пол: мужской, возраст: 5, отец: 1, мать: 2
Имя: 5, пол: мужской, возраст: 5, отец: 1, мать: 2
Но ведь я не вызывал этот метод
Human h1 = new Human("1", true, 1);
Human h2 = new Human("2", true, 2);
Human h3 = new Human("3", false, 3);
Human h4 = new Human("4", false, 4);
Human h5 = new Human("5", true, 5, h1, h2);
Human h6 = new Human("5", true, 5, h1, h2);
Human h7 = new Human("5", true, 5, h1, h2);
Human h8 = new Human("5", true, 5, h1, h2);
Human h9 = new Human("5", true, 5, h1, h2);
System.out.println(h1);
System.out.println(h2);
System.out.println(h3);
System.out.println(h4);
System.out.println(h5);
System.out.println(h6);
System.out.println(h7);
System.out.println(h8);
System.out.println(h9);
}
public static class Human {
public String name = null;
public boolean sex;
public int age;
public Human father;
public Human mother;
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 (this.father != null) {
text += ", отец: " + this.father.name;
}
if (this.mother != null) {
text += ", мать: " + this.mother.name;
}
return text;
}
}
}