[童虎退壳系列]判等与哈希值的覆写

public final class EqualsHashCode {
	final int x;
	final int y;

	public EqualsHashCode(int x, int y) {
		this.x = x;
		this.y = y;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;

		if (obj instanceof EqualsHashCode) {
			System.out.println("instanceof test successfully.");
			EqualsHashCode other = (EqualsHashCode) obj;
			return this.x == other.x && this.y == other.y;
		} else if (obj == null) {
			System.out.println("instanceof test a null object is not raised an exception.");
			return false;
		}
		return true;
	}

	@Override
	public int hashCode() {
		return x ^ y;
	}

	@Override
	public String toString() {
		return "x=" + x + "y=" + y;
	}

	public static void main(String[] args) {
		EqualsHashCode thiz1 = new EqualsHashCode(1, 2);
		EqualsHashCode thiz2 = new EqualsHashCode(2, 1);
		EqualsHashCode thiz3 = new EqualsHashCode(2, 1);
		thiz1.equals(null);
		System.out.println(thiz1.hashCode());
		System.out.println(thiz2.hashCode());
		System.out.println(thiz1.equals(thiz2));
		System.out.println(thiz3.equals(thiz2));
	}
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值