Компилятор последней строкой выводит деда, красным цветом. Выводит верно, НО красным цветом и намекает, что чтото не так.
Я в замешательстве.
package com.javarush.task.jdk13.task08.task0824;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/*
Вся семья в сборе
*/
public class Solution {
public static void main(String[] args) {
//напишите тут ваш код
Human child1 = new Human("Ребёнок1", true, 23);
Human child2 = new Human("Ребёнок2", false, 4);
Human child3 = new Human("Ребёнок3", true, 1);
Human father = new Human("Отец", true, 52, child1, child2, child3);
Human mother = new Human("Мама", false, 49, child1, child2, child3);
Human grandFatherMother = new Human("Дедушка1", true, 90, mother);
Human grandMotherMother = new Human("Бабушка1", false, 86, mother);
Human grandFatherFather = new Human("Дедушка2", true, 150, father);
Human grandMotherFather = new Human("Бабушка2", false, 112, father);
System.out.println(grandFatherFather.toString());
System.err.println(grandFatherMother.toString());
System.out.println(grandMotherFather.toString());
System.out.println(grandMotherMother.toString());
System.out.println(father.toString());
System.out.println(mother.toString());
System.out.println(child1.toString());
System.out.println(child2.toString());
System.out.println(child3.toString());
}
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, boolean sex, int age, Human... children) {
this.name = name;
this.sex = sex;
this.age = age;
Collections.addAll(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;
}
}
}