java基础[Object类中包含的方法]

package com.hike.javase.object;
/*
 * boolean equals(Objecr obj):
 * 		判断当前对象中的内容是否和参数中的对象内容相同
 * 
 * public int hashCode():计算出对象的哈希码(散列码或特征码):
//		根据对象的内容创建出来的特征码值(根据某种规则),对象内容相同则哈希码必须相同,反之亦同
 * 
 * public String toString():把对象变成字符串,字符串内容是对象的详细信息
 * 		打印对象、完成字符串拼接时,会自动调用toString()方法
 */
public class Point {
	private int x;
	private int y;
	
	public Point() {
		super();
	}
	public Point(int x, int y) {
		super();
		this.x = x;
		this.y = y;
	}
	public int getX() {
		return x;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getY() {
		return y;
	}
	public void setY(int y) {
		this.y = y;
	}
	
	public String say() {
		return "x:" + x + ",y:" + y;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Point other = (Point) obj;
		if (x != other.x)
			return false;
		if (y != other.y)
			return false;
		return true;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + x;
		result = prime * result + y;
		return result;
	}
	@Override
	public String toString() {
		return "Point [x=" + x + ", y=" + y + ", getX()=" + getX() + ", getY()=" + getY() + ", say()=" + say()
				+ ", hashCode()=" + hashCode() + ", getClass()=" + getClass() + ", toString()=" + super.toString()
				+ "]";
	}
}


package com.hike.javase.object;

/*
 * boolean equals(Objecr obj):
 * 		判断当前对象中的内容是否和参数中的对象内容相同,但它其实是一个虚方法,需要根据需求的不同,更改其内容
 */

public class PointTest {
	public static void main(String[] args) {
		Point p1 = new Point(11,22);
		Point p2 = new Point(13,20);
		
		boolean res = p1.equals(p2);
		System.out.println(res);
		
//		哈希码(散列码或特征码):
//			根据对象的内容创建出来的特征码值(根据某种规则),对象内容相同则哈希码必须相同,反之亦同
		System.out.println(p1.hashCode());
		System.out.println(p2.hashCode());
		
		System.out.println(p1);
		
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

OneTenTwo76

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值