------------------------------------------------Animal.java-------------------------------------------
package com.imooc;
public class Animal extends Object {
public int age=20;
public String name;
public void eat(){
System.out.println("年龄"+age+"动物具有吃东西的能力");
}
public Animal(){
System.out.println("Animal类执行了");
}
}
----------------------------------------------Dog.java------------------------------------
package com.imooc;
public class Dog extends Animal {
public int age=20;
@Override
public boolean equals(Object obj) {
if (this == obj)//两个引用的地址是否相同
return true;
if (!super.equals(obj))//两个对象是否是空值
return false;
if (getClass() != obj.getClass())//类对象,两个对象的类型
return false;
Dog other = (Dog) obj;
if (age != other.age)
return false;
return true;
}
}
------------------------------------------------Initail.java---------------------------------------------------
package com.imooc;
public class Initail {
public static void main(String[] args) {
// TODO Auto-generated method stub
Dog dog = new Dog();
Dog dog1 = new Dog();
if(dog.equals(dog1)){
System.out.println("两个对象是相同的");
}else{
System.out.println("两个对象是不同的");
}
}
}
----------------------------------------------输出结果---------------------------------------------
Animal类执行了
Animal类执行了
两个对象是不同的
PS:老师显示是相同的,我是不同的
guozhchun
可爱的龟龟
相关分类