Помогите разобрать в следующих вопросах:
1) не понимаю, когда начинает вызываться метод toString(подозреваю, что вызывается неявно при выводе на экран, прав ли я)?
2) не понимаю как в этом методе надо обращаться к переменным объекта, когда определяется что выводить на экран- через this или же просто указывать поле. В любом случае не проходит валидацию и после бабушки и дедушки выводит уже на экран не пойми что
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 grandfather = new Cat(grandfatherName);
String grandmotherName = reader.readLine();
Cat grandmother = new Cat(grandmotherName);
String fatherName = reader.readLine();
Cat father = new Cat(fatherName,null,grandfatherName);
String motherName = reader.readLine();
Cat mother = new Cat(motherName,grandmotherName,null);
String sonName = reader.readLine();
Cat son = new Cat(sonName,motherName,fatherName);
String daughterName = reader.readLine();
Cat daughter = new Cat(daughterName,motherName,fatherName);
System.out.println(grandfather);
System.out.println(grandmother);
System.out.println(father);
System.out.println(mother);
System.out.println(son);
System.out.println(daughter);
}
public static class Cat {
private String name;
private String mother;
private String father;
public Cat(String name) {
this.name = name;
}
public Cat(String name,String mother, String father) {
this.name = name;
this.mother =mother;
this.father = father;
}
@Override
public String toString() {
if ((this.mother == null)&&(this.father == null))
return ("The cat's name is " + this.name + ", no mother "+ ", no father ");
if ((this.mother.length()>0)&&(this.father == null))
return ("The cat's name is " + this.name + ", mother is " + this.name+ ", no father ");
if ((this.father.length()>0)&&(this.mother == null))
return "The cat's name is " + this.name + ",no mother " + ", father is "+this.name;
if ((this.father.length()>0)&&(this.mother.length()>0))
return "The cat's name is " + this.name + ", mother is " + this.name+ ", father is "+this.name;
else return null;
}
}
}