Что-то сложно идёт эта задача для меня, в методе toString() в частности компилятор находит ошибку this.children.length. Не могу понять почему и что ему надо?
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 grandpa1 = new Human("Николай", true, 67, new Human("Сергей", true, 40));
Human grandpa2 = new Human("Генадий", true, 68, new Human("Светлана", false, 30));
Human grandma1 = new Human("Лида", false, 62, new Human("Сергей", true, 40));
Human grandma2 = new Human("Екатерина", false, 63, new Human("Светлана", false, 30));
Human father = new Human("Сергей", true, 40, new Human("Валентин", true, 5), new Human("София", false, 7), new Human("Борис", true, 3));
Human mother = new Human("Светлана", false, 30, new Human("Валентин", true, 5), new Human("София", false, 7), new Human("Борис", true, 3));
}
public static class Human {
//напишите тут ваш код
String name;
boolean sex;
int age;
ArrayList<Human> children = new ArrayList<>();
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.length;
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;
}
}
}