• 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("不是女的!");
    }
    }

    }



  • 相关阅读:
    #include <utility>
    Html的空格显示
    ExtJs自学教程(1):一切从API開始
    天黑的时候,我又想起那首歌
    citrix协议ICA技术原理
    约瑟夫环问题
    数据结构和算法设计专题之---八大内部排序
    HDU
    深入分析C++引用
    八大排序算法总结
  • 原文地址:https://www.cnblogs.com/andgoo/p/2433856.html
Copyright © 2020-2023  润新知