здравствуйте))) спасибо за задачку про кошки. подскажите пожалуйста почему когда первым создается объект кошки -бабушки в моем коде то при создании кота-отца поле
private String catMother; инициализируется а поле catFather нет??
Вот мой код:
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 gMother = reader.readLine();
Cat catGMother = new Cat(gMother, null, null);
String gFather = reader.readLine();
Cat catGFather = new Cat(gFather, null, null);
String father = reader.readLine();
Cat catFather = new Cat(father, gFather, null);
String mother = reader.readLine();
Cat catMother = new Cat(mother, null, gMother);
String sun = reader.readLine();
Cat catSun = new Cat(sun, father, mother);
String doughter = reader.readLine();
Cat catDoughter = new Cat(doughter, father, mother);
System.out.println(catGMother);
System.out.println(catGFather);
System.out.println(catFather);
System.out.println(catMother);
System.out.println(catSun);
System.out.println(catDoughter);
}
public static class Cat {
private String name;
private String catFather;
private String catMother;
Cat(String name, String catFather, String catMother)
{
this.name = name;
this.catFather = catFather;
System.out.println(catFather);
this.catMother = catMother;
System.out.println(catMother);
}
@Override
public String toString() {
if(catFather==null && catMother==null)
return "Cat name is " + name + ", no mother no father";
if(catMother==null)
return "Cat name is "+ name + ", no mother, " + " father is "+catFather;
if(catFather==null)
return "Cat name is "+ name + ", no father " + " mother is "+catMother;
else
return "Cat name is "+ name + " mother is " + catMother+ " father is "+catFather;
}
}
}
и резултат:
дед Вася
null
null
бабушка Маря
null
null
папа Федор
бабушка Маря
null
мама Маша
null
дед Вася
брат
папа Федор
мама Маша
сестра
папа Федор
мама Маша
Cat name is дед Вася, no mother no father
Cat name is бабушка Маря, no mother no father
Cat name is папа Федор, no mother, father is бабушка Маря
Cat name is мама Маша, no father mother is дед Вася
Cat name is брат mother is мама Маша father is папа Федор
Cat name is сестра mother is мама Маша father is папа Федор