Пробовал по-разному, но всегда ошибка.
package com.javarush.task.task08.task0824;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
/*
Собираем семейство
*/
public class Solution {
public static void main(String[] args) {
Human son1 = new Human("Александр",true,13);
Human son2 = new Human("Олег",true,6);
Human son3 = new Human("Виктор",true,3);
List<Human> childrens = new ArrayList<>();
childrens.add(son1);
childrens.add(son2);
childrens.add(son3);
Human dad = new Human("Вася",39,true,childrens);
Human mom = new Human("Светлана",39,false,childrens);
Human grannyM = new Human("Зинаида",83,false, (List<Human>) mom);
Human grannyD = new Human("Зинаида",71,false, (List<Human>) dad);
Human grandadM = new Human("Пётр",86,true, (List<Human>) mom);
Human grandadD = new Human("Денис",59,true, (List<Human>) dad);
System.out.println(grandadD);
System.out.println(grandadM);
System.out.println(grannyD);
System.out.println(grannyM);
System.out.println(mom);
System.out.println(dad);
System.out.println(son1);
System.out.println(son2);
System.out.println(son3);
}
public static class Human {
String name;
boolean sex;
int age;
List<Human> children = new ArrayList<>();
public Human (String name, boolean sex, int age){
this.name = name;
this.sex = sex;
this.age = age;
}
public Human(String name, int age, boolean sex, List<Human> children){
this.name = name;
this.age = age;
this.sex = sex;
this.children = children;
}
public String toString() {
String text = "";
text += "Имя: " + this.name;
text += ", пол: " + (this.sex ? "мужской" : "женский");
text += ", возраст: " + this.age;
int childCount = this.children.size();
if (childCount > 0) {
text += ", дети: " + this.children.get(0).name;
for (int i = 1; i < childCount; i++) {
Human child = this.children.get(i);
text += ", " + child.name;
}
}
return text;
}
}
}