package Equals的用法;
public class Phone1 {
public int cpu = 2;
public void say() {
System.out.println("我是手机");
}
}
package Equals的用法;
public class Phone2 {
public int cpu = 2;
public void say() {
System.out.println("我是手机");
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Phone2 other = (Phone2) obj;
if (cpu != other.cpu)
return false;
return true;
}
}
package Equals的用法;
public class User {
public static void main(String[] args) {
Phone1 aPhone1 = new Phone1();
Phone2 aPhone2 = new Phone2();
if (aPhone1.equals(aPhone2)) {
System.out.println("比较的是对象的地址: 一样");
} else {
System.out.println("比较的是对象的地址: 不一样");
}
if (aPhone2.equals(aPhone1)) {
System.out.println("比较的是对象的属性: 一样");
} else {
System.out.println("比较的是对象的属性: 不一样");
}
}
}
Caballarii
相关分类