覆盖Object类的equals、hashCode和toString方法

1.equals方法
equals方法和“==”进行比较的时候,引用类型数据比较的是引用,即内存地址,基本数据类型比较的是值。
下面进行覆盖此方法,使其只比较对象属性值。
代码如下:

/**
* 覆盖前equals和“==”比较的都是内存地址
* 覆盖equals方法,覆盖后的equals比较的是值,== 比较的是内存地址
*/
public class CoverEquals{
    public static void main(String[] args){
        Equals e1 = new Equals();
        Equals e2 = new Equals();
        e1.id = 100;
        e2.id = 100;

        System.out.println(e1 == e2);
        System.out.println(e1.equals(e2));
    }
}
class Equals{
    public int id = 0;  //作为对象属性值使用
    //复写equals()
    public boolean equals(Object obj){  //形参
        if(this == obj){    //第一步,判断是否是同一个实例
            return true;
        }

        if(obj == null){    //第二步,判断要比较的对象是否为null
            return false;
        }

        if(obj instanceof Equals){  //第三步,判断是否是同一个类型
            //Equals e = (Equals)obj;   //第四步,类型相同,转换为同一个类型
            if(this.id == obj.id){  //第五步,进行对象属性值的比较
                return true;
            }else{
                return false;
            }
        }else{
            return false;   //类型不同,直接返回false
        }
    }
}

2.hashCode方法
hashCode是按照一定的算法得到的一个数值,是对象的散列码值。主要用来在集合中实现快速查找等操作。
Java规范里面规定,覆盖equals方法应该连带覆盖hashCode方法。
下面程序是采用一定算法来实现hashCode方法:

/**
* 覆盖equals时,应该连带覆盖hashCode方法
* 以下是eclipse自动生成的
*/
import java.util.Arrays;

public class HashCodeTest{
    private byte byteValue;
    private char charValue;
    private short shortValue;
    private int intValue;
    private long longValue;
    private boolean booleanValue;
    private float floatValue;
    private double doubleValue;
    private String uuid;
    private int[] intArray = new int[3];

    public int hashCode(){
        final int prime = 31;
        int result = 1;
        result = prime * result + (booleanValue ? 1231 : 1237);
        result = prime * result + charValue;

        long temp;
        temp = Double.doubleToLongBits(doubleValue);
        result = prime * result + (int)(temp ^ (temp >>> 32));
        result = prime * result + Float.floatToIntBits(floatValue);
        result = prime * result + Arrays.hashCode(intArray);
        result = prime * result + intValue;
        result = prime * result + (int)(longValue ^ (longValue >>> 32));
        result = prime * result + shortValue;
        result = prime * result + ((uuid == null) ? 0 : uuid.hashCode());
        return result;
    }
}

3.toString方法也是Object类的方法,覆盖它可以让你得到更适用的返回信息。
代码示例如下:

/**
* 覆盖toString方法
*/
public class CovertoString{
    public static void main(String[] args){
        MyInformation m = new MyInformation("dongshuai","18800000000",180);
        System.out.println(m);
        System.out.println(m.toString());

        YourInformation y = new YourInformation("dongshuai","18800000000",180);
        System.out.println(y);
    }
}
class MyInformation{
    private String name;
    private String phone;
    private int stature;
    public MyInformation(String name,String phone,int stature){
        this.name = name;
        this.phone = phone;
        this.stature = stature;
    }
}
class YourInformation{
    private String name;
    private String phone;
    private int stature;
    public YourInformation(String name,String phone,int stature){
        this.name = name;
        this.phone = phone;
        this.stature = stature;
    }
    public String toString(){
        return name + "<-->" + phone + "<-->" + stature;
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值