Не понимаю, почему не отображаются дети у родителей.
Как я понял суть задачи в следующем:
- создаём 3 детей;
- 2 родителей в которых передаём этих детей
- 2 бабушек и дедушек в которых передаём родителей, как детей.
Также, есть вопрос - как добавить ребёнка в массив?
package com.javarush.task.task08.task0824;
import java.util.ArrayList;
import java.util.List;
/*
Собираем семейство
*/
public class Solution {
public static void main(String[] args) {
Human children1 = new Human(1, "Jack", true);
Human children2 = new Human(2, "Leon", true);
Human children3 = new Human(1, "Ada", false);
ArrayList <Human> children = new ArrayList<>();
children.add(children1);
Human Dad = new Human(32, "Solid", true, children1);
System.out.println(Dad.toString());
}
public static class Human {
int age;
String name;
boolean sex;
ArrayList <Human> children = new ArrayList<>();
public Human (int age, String name, boolean sex) {
this.age = age;
this.name = name;
this.sex = sex;
}
public Human (int age, String name, boolean sex, Human children) {
this.age = age;
this.name = name;
this.sex = sex;
this.children = new ArrayList<Human>();
}
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;
}
}
}