Как в этом коде так получается, что
mother (this.mother) ассоциируется и с catMother и с catGrandMother,
а name(this.name) вообще ссылается на имена всех котов и кошек и каким то образом в итоге правильно их раставляет при выводе не печать
Не могу уловить, какая тут связь?
String grandFatherName = reader.readLine();
Cat catGrandFather = new Cat(grandFatherName, null, null);
String grandMotherName = reader.readLine();
Cat catGrandMother = new Cat(grandMotherName, null, null);
String fatherName = reader.readLine();
Cat catFather = new Cat(fatherName, null, catGrandFather);
String motherName = reader.readLine();
Cat catMother = new Cat(motherName, catGrandMother, null);
String sonName = reader.readLine();
Cat catSon = new Cat(sonName, catMother, catFather);
String daughterName = reader.readLine();
Cat catDaughter = new Cat(daughterName, catMother, catFather);
System.out.println(catGrandFather.toString());
System.out.println(catGrandMother.toString());
System.out.println(catFather.toString());
System.out.println(catMother.toString());
System.out.println(catSon.toString());
System.out.println(catDaughter.toString());
}
public static class Cat {
private String name;
private Cat mother;
private Cat father;
Cat(String name) {
this.name = name;
}
Cat(String name, Cat mother, Cat father) {
this.name = name;
this.mother = mother;
this.father = father;
}