首页 > 其他 > 详细

5-2 equal方法

时间:2018-01-02 14:47:43      阅读:207      评论:0      收藏:0      [点我收藏+]
package fivetwo;
public class ObjectExtend extends Object{

    public static void main(String[] args) {

    }
    public static boolean equals(Object a,Object b){
        return a==b || (a!=null && a.equals(b));  
    }
}




package fivetwo;
public class Employee{
    private String str;
    private int x;
    
    public static void main(String[] args) {
        
    }
    
    public static void method1(){
    }
    public boolean equals(Object otherObject){
        
        if (this == otherObject) {
            return true;
        }
        if (otherObject == null) {
            return false;
        }
        if (!(getClass() == otherObject.getClass())) {
            return false;
        }
        
        Employee other= (Employee)otherObject;
//        return (this.x == other.x && this.str.equals(other.str));
        return (this.x == other.x && ObjectExtend.equals(this.str, other.str));
    }
    
    //Objects.equals jdk的高版本非空判断
}



package fivetwo;

public class Manager extends Employee{

    private int bonus;
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Manager m1 = new Manager();
        Manager m2 = new Manager();
        m1.equals(m2);

    }
    
    //子类的比较先调用超类的equal方法,超类的域不相等就不可能相等
    public boolean equals(Object otherObject){
        if (!super.equals(otherObject)) {
            return false;
        }
        
        Manager other = (Manager)otherObject;
        return this.bonus == other.bonus;
    }
    

}

 

5-2 equal方法

原文:https://www.cnblogs.com/lxh520/p/8176775.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!