Practical Java笔记二:不要使用缺省的equal方法

/**
*
* @ClassName: GolfBall
* @Description: 高尔夫球类
* @date 2012-4-5 下午10:11:30
*/
class GolfBall{
private String brand; //品牌
private String make; //型号
public GolfBall(String brand, String make) {
super();
this.brand = brand;
this.make = make;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}

@Override
public boolean equals(Object obj) {
if(this == obj)
return true;
if(obj!=null && getClass() == obj.getClass()){
GolfBall gb = (GolfBall)obj;
if(this.brand.equals(gb.getBrand()) && this.make.equals(gb.getMake())){
return true;
}
}
return false;
}
public class EqualDemo {
public static void main(String[] args) {
GolfBall gb1 = new GolfBall("谢仕玲", "女");
GolfBall gb2 = new GolfBall("谢仕玲","女");
if(gb1.equals(gb2)){
System.out.println("的确是女的!");
}else{
System.out.println("不是女的!");
}
}

}



原文地址:https://www.cnblogs.com/andgoo/p/2433856.html