zoukankan      html  css  js  c++  java
  • 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("不是女的!");
    }
    }

    }



  • 相关阅读:
    IGV解读
    box-cox解读
    linux命令eval的用法
    R中导入excel乱码的解决办法
    Django下实现HelloWorld
    python的list求和与求积
    win10下安装Django
    python下实现汉诺塔
    (stm32f103学习总结)—DS18B20
    (stm32f103学习总结)—GPIO结构
  • 原文地址:https://www.cnblogs.com/andgoo/p/2433856.html
Copyright © 2011-2022 走看看