package zh.codegym.task.task08.task0824;
/*
构建家庭
*/
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) {
//在此编写你的代码
ArrayList<Human> childrens=new ArrayList<Human>();
Human child1 = new Human("childnumberone",false,1,childrens);
System.out.println( child1.toString());
Human child2 = new Human("childnumbertwo",false,1,childrens);
System.out.println( child2.toString());
Human child3 = new Human("childnumberthree",false,1,childrens);
System.out.println( child3.toString());
childrens.add(child1);
childrens.add(child3);
childrens.add(child2);
Human grandfather= new Human("汤姆",false,80,childrens);
System.out.println( grandfather.toString());
Human grandfather2= new Human("汤姆2",false,81,childrens);
System.out.println( grandfather2.toString());
Human grandmother= new Human("珍妮",false,82,childrens);
System.out.println( grandmother.toString());
Human grandmother2= new Human("珍妮2",false,83,childrens);
System.out.println( grandmother2.toString());
Human father = new Human("father",true,20,childrens);
System.out.println( father.toString());
Human mother = new Human("mother",false,25,childrens);
System.out.println(mother.toString());
}
public static class Human {
//在此编写你的代码
public String name;
public boolean sex;
public int age;
public ArrayList<Human> children;
public Human(String name, boolean sex, int age, ArrayList<Human> children) {
this.name = name;
this.sex = sex;
this.age = age;
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;
}
}
}