/*
Object类是所有对象的父类,最高级
该类中定义的是所有类都具有的功能;
1、方法equals比较的是对象的内存地址。
*/
class ObjectDemo
{
private int num;
public boolean equals(Object obj) //重写Object的equals方法
{
if((obj instanceof ObjectDemo))
{
ObjectDemo od=(ObjectDemo)obj;
return this.num==od.num;
}
else
{
return false;
}
}
}
class EqualsDemo
{
public static void main(String[] args)
{
ObjectDemo od1=new ObjectDemo();
ObjectDemo od2=new ObjectDemo();
ObjectDemo od3=od1;
System.out.println(od1.equals(od2));
System.out.println(od1==od2); //比较地址
System.out.println(od1.equals(od3));
<span style="white-space:pre"> System.out.println(od1.toString()); </span>//转换为字符串<span style="white-space:pre">
<span style="white-space:pre"> </span>System.out.println(Integer.toHexString(od1.hashCode())); </span>//获取对象的哈希值
/*
--------- 运行 ----------true
false
true
ObjectDemo@16dadf9
16dadf9
输出完成 (耗时 0 秒) - 正常终止
*/
}
}