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))
return false;
Demo d = (Demo)obj;
return this.num == d.num;
}
public String toString()
{
return "demo:"+num;
}
}
class Person {}
class ObjectDemo
{
public static void main(String[] args)
{
Demo d1 = new Demo(4);
System.out.println(d1);//系统默认地会调用对象的toString()方法输出结果,多疑结果是demo:4
Demo d2 = new Demo(7);
System.out.println(d2.toString());//当程序执行到System.out.println(d2 时,d2 = new Demo(7),这是程序就跳转到Demo类去执行,
//最后还是调用toString();方法执行输出.
}
}
其实老师上课的时候讲过,编译看左边,执行看右边 |