//仅作为学习笔记
/*
Object : 是所有对象的直接或者间接父类
该类中定义的肯定是所有对象都具备的功能
Object类中已经提供了对对象是否相同的比较方法
如果自定义类中也有比较相同的功能 ,没有必要重新定义
只要沿袭父类中的功能,建立自己特有的比较内容即可,这就是覆盖
*/
class Demo //extends Object
{
private int num ;
Demo(int num)
{
this.num = num;
}
public boolean equals(Object obj)//Object obj = new Demo();
{
if( !(obj instanceof Demo))//判断偶obj是否指向Demo类
return false;
Demo d = (Demo)obj;
return this.num == d.num;
}
}
class Person
{
private int num;
Person(int num)
{
this.num = num;
}
}
class Test
{
public static void main(String []args)
{
Demo d1 = new Demo(3);
Demo d2 = new Demo(4);
Demo d3 = new Demo(3);
System.out.println(d1.equals(d2));
System.out.println(d1.equals(d3));
Person p = new Person(4);
System.out.println(d1.equals(p));
}
}
![](https://img-my.csdn.net/uploads/201303/04/1362395842_2836.jpg)