运行不出正确答案
DOG类中:
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Dog other = (Dog) obj;
if (age != other.age)
return false;
return true;
}
调用的类:
package com.exe02;
public class Inital {
public static void main(String[] args){
Dog dog=new Dog();
dog.age=60;
Dog dog2=new Dog();
dog2.age=70;
if(dog.equals(dog2)){
System.out.println("两个对象相同");
}
else{
System.out.println("两个对象不同");
}
}
}
结果还是不同