为什么我这样写 自动生成的是Animal other = (Animal) obj;转换成Animal类 而不是转换为Dog类
package test;
public class Animal {
int age;
String shout;
String name;
String eat;
public void a1(){
System.out.println("这只"+name+"年龄大概是"+age+"它的叫声是"+shout+"它们主要吃"+eat);
}
public class Dog extends Animal{
String kind;
public void a1() {
System.out.println("这只"+name+"年龄大概是"+age+"它的叫声是"+shout+"它们主要吃"+eat+"它们总是让人感觉很"+kind);
}
}
public static void main(String[] args) {
Animal a = new Animal();
Dog p1 = a.new Dog();
Dog p2 = a.new Dog();
if(p1.equals(p2)) {
System.out.println("两个对象相同;");
}
else {
System.out.println("两个对象不同;");
}
p1.age= 5;
p1.name= "小狗";
p1.shout= "汪汪汪";
p1.eat = "肉、蔬菜";
p1.kind = "友好";
//p1.a1();
//System.out.println(p1);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Animal other = (Animal) obj;
if (age != other.age)
return false;
if (eat == null) {
if (other.eat != null)
return false;
} else if (!eat.equals(other.eat))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (shout == null) {
if (other.shout != null)
return false;
} else if (!shout.equals(other.shout))
return false;
return true;
}
}