Нужно как-то разграничить
Cat(String name, Cat mother) {
this.name = name;
this.mother = mother;
}
Cat(String name, Cat father) {
this.name = name;
this.father = father;
}
Но в такой вариации код дальше конструктора со второй переменной mother не будет доходить и будет выдавать ошибкуpackage com.javarush.task.task06.task0621;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
Родственные связи кошек
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String grandfatherName = reader.readLine();
Cat catGrandfather = new Cat(grandfatherName);
String grandmatherName = reader.readLine();
Cat catGrandmather = new Cat(grandmatherName);
String fatherName = reader.readLine();
Cat catFather = new Cat(fatherName, this.mother, catGrandfather);
String motherName = reader.readLine();
Cat catMother = new Cat(motherName, catGrandmather, this.Father);
String sonName = reader.readLine();
Cat sonCat = new Cat(sonName, catMother, catFather);
String daughterName = reader.readLine();
Cat daughterCat = new Cat(daughterName, catMother, catFather);
System.out.println(catGrandfather);
System.out.println(catGrandmather);
System.out.println(catFather);
System.out.println(catMother);
System.out.println(sonCat);
System.out.println(daughterCat);
}
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;
}
@Override
public String toString() {
if (mother == null && father == null){
return "The cat's name is " + name + ", no mother, no father ";
}
if(father == null && mother != null){
return "The cat's name is " + name + ", mother is " + mother.name + ", no father ";
}
if(father != null && mother != null){
return "The cat's name is " + name + ", father is " + father.name + ", no mother ";
}
else {
return "The cat's name is " + name + ", mother is " + mother.name + ", father is " + father.name;
}
}
}
}