Гляньте люды добрые в чём тут дело, по идее выводит все объекты согласно условию задачи
package com.javarush.task.task08.task0824;
/*
Собираем семейство
*/
import java.util.ArrayList;
import java.util.Collections;
public class Solution {
static ArrayList<Human> family = new ArrayList<>();
public static void main(String[] args) {
Human son = new Human("Serj", true, 12);
Human dother = new Human("Kate", false, 10);
Human dother2=new Human("Liza", false, 15);
Human mama = new Human("Sara", false, 33, son, dother2, dother);
Human papa=new Human("Bobs jr.", true, 40, son, dother2, dother);
Human grandmaMa=new Human("Umi", false, 99, papa);
Human grandmaMother = new Human("Tekilla", false, 153, mama);
Human grandpaMother=new Human("Lindon", true, 103, mama);
Human grandpaPa=new Human("Bobbs", true, 66, papa);
Collections.reverse(family);
for(Human human:family) System.out.println(human);
}
public static class Human {
String name;
boolean sex;
int age;
ArrayList<Human> children=new ArrayList<>();
public Human(String name, boolean sex, int age){
this.age=age;
this.name=name;
this.sex=sex;
family.add(Human.this);
}
public Human(String name, boolean sex, int age, Human human){
this.age=age;
this.name=name;
this.sex=sex;
children.add(human);
family.add(Human.this);
}
public Human(String name, boolean sex, int age, Human human, Human human2, Human human3){
this.age=age;
this.name=name;
this.sex=sex;
children.add(human);
children.add(human2);
children.add(human3);
family.add(Human.this);
}
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;
}
}
}