为什么我这里两对象不同类型相同属性的值用equals()方法判断是相同的呢?
public class Phone {
int screen=1;
public float a() {
float screen=1;
return screen;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Phone other = (Phone) obj;
if (screen != other.screen)
return false;
return true;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Phone phone1=new Phone();
phone1.screen=1;
Phone phone2=new Phone();
phone2.a();
if(phone1.equals(phone2))
System.err.println("两对象相同 phone1.screen:"+phone1.screen+" phone2.a():"+phone2.a());
else
System.out.println("两对象不相同 phone1.screen:"+phone1.screen+" phone2.a():"+phone2.a());
}
}
视频里老师说equals()方法里判断类型不同会返回false 但我这段代码不同类型也返回true 我哪里理解错了呢?