zoukankan      html  css  js  c++  java
  • java完美equals方法代码段

    public boolean equals(Object otherObject) {
        	if(this == otherObject) {    // 检測this与otherObject是否引用同一个对象
        		return true;
        	}
    
        	if(null == otherObject ) {   // 检測otherObject是否为空
        		return false;
        	}
    
        	if(!(getClass() == otherObject.getClass())){  // 比較this与oherObject是否属于同一个类,假设equal的语义在每一个子类中有所改变,就用此推断
        		System.out.println("-----------------getClass----------------");
        		return false;
        	}
    
        	if( ! (otherObject instanceof Apple)) {  // 假设语义同样就用instanceof推断,推断继承时也用到
        		System.out.println("------------instanceof--------------------");
        		return false;
        	}
    
        	Apple other = (Apple) otherObject;  // 转换为对应类型。对所需域进行推断
    
        	return name.equals(other.name)&& color.equals(other.color);
         }
    }

  • 相关阅读:
    Codeforces_739_B
    Codeforces_732_D
    D
    C
    E
    商汤AI园区的n个路口(中等)
    D. The Fair Nut and the Best Path
    HDU6446
    分解质因数(线性筛)
    D. Extra Element
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5070875.html
Copyright © 2011-2022 走看看