用重寫(xiě)equals()方法比較兩個(gè)手機(jī),屬性都相同,運(yùn)行結(jié)果怎么是不同的
package com.imooc;
public class infial {
public static void main(String[] args) {
// TODO Auto-generated method stub
Telephone tele=new Telephone();
tele.size=10;
tele.CPU=10;
tele.Memory=10;
Telephone tele1=new Telephone();
tele.size=10;
tele.CPU=10;
tele.Memory=10;
if(tele.equals(tele1)){
System.out.println("兩個(gè)對(duì)象是相同的");
}
else{
System.out.println("兩個(gè)對(duì)象是不相同的");
}
}
}
package com.imooc;
public class Telephone {
? ? ? ? ?int size;
? ? ? ? ?int CPU;
? ? ? ? ?int Memory;
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Telephone other = (Telephone) obj;
if (CPU != other.CPU)
return false;
if (Memory != other.Memory)
return false;
if (size != other.size)
return false;
return true;
}
? ? ? ? ?
}
2018-03-04
問(wèn)題在給第二個(gè)tele1你寫(xiě)成了tele