码迷,mamicode.com
首页 > 其他好文 > 详细

5-2 equal方法

时间:2018-01-02 15:20:00      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:gpo   xtend   nbsp   objects   equal   ack   package   new   pre   

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方法

标签:gpo   xtend   nbsp   objects   equal   ack   package   new   pre   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!