public class Solution {
public static void main(String[] args) {
ArrayList<String> family=new ArrayList<String>();
Human wasya=new Human("Вася", true, 82);
family.add(Human.wasya); // КАК ПРАВИЛЬНО ВНЕСТИ В СПИСОК ЭТИ ОБЪЕКТЫ?
Human toma=new Human("Тома", false, 78);
Human gena=new Human("Гена", true, 80);
Human olya=new Human("Оля", false, 76);
Human andrey=new Human("Андрей", true, 55, wasya, toma);// Лена Слава Женя Настя
Human lena=new Human("Лена", false, 50, gena, olya);
Human slava=new Human("Слава", true, 30, andrey, lena);
Human evgen=new Human("Женя", true, 25, andrey, lena);
Human nastya=new Human("Настя", true, 24, andrey, lena);
for (String n: family){
System.out.println(n);
}
}
public static class Human {
String name;
boolean sex;
int age;
Human father;
Human mother;
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 father, Human mother){
this.name=name;
this.sex=sex;
this.age=age;
this.father=father;
this.mother=mother;
}
public String toString() {
String text = "";
text += "Имя: " + this.name;
text += ", пол: " + (this.sex ? "мужской" : "женский");
text += ", возраст: " + this.age;
if (this.father != null)
text += ", отец: " + this.father.name;
if (this.mother != null)
text += ", мать: " + this.mother.name;
return text;
}
}
}
Евгений
8 уровень
решил задачу примитивно. после чего подумал что это всё можно внести в список. но не могу понять как. помогите пожалуйста
Решен
Комментарии (2)
- популярные
- новые
- старые
Для того, чтобы оставить комментарий Вы должны авторизоваться
IceBergMaster
22 апреля 2019, 10:28
Тебе нужен список Human-ов, а не строк. Но учитывая, что ты за объектами закрепляешь переменные, то от списка толку мало.
0
Евгений
24 апреля 2019, 12:29
понял спасибо
0