Семейная перепись
Создай класс Human с полями имя(String), пол(boolean), возраст(int), отец(Human), мать(Human). Создай объекты и заполни их так, чтобы получилось: Два дедушки, две бабушки, отец, мать, трое детей. Вывести объекты на экран.
Примечание:
Если написать свой метод String toString() в классе Human, то именно он будет использоваться при выводе объекта на экран.
package com.javarush.task.task07.task0724;
/*
Семейная перепись
*/
public class Solution {
public static void main(String[] args) {
Human ded1 = new Human("fd",true,55);
Human bab1 = new Human("fdds",false,60);
Human ded2 = new Human("fdd",true,57);
Human bab2 = new Human("fddd",false,59);
Human dad = new Human("fdsfse",true,25);
Human mom = new Human("fddasfasf",false,30);
Human child = new Human("fdsasa",false,10);
Human child2 = new Human("fddss",false,11);
Human child3 = new Human("fddddddd",false,12);
System.out.println(ded1);
System.out.println(bab1);
System.out.println(ded2);
System.out.println(bab2);
System.out.println(dad);
System.out.println(mom);
System.out.println(child);
System.out.println(child2);
System.out.println(child3);
}
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;
}
}
}