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/archive/2012/04/05/2433856.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值