package cn.it_02;
public class ObjectDemo {
public static void main(String[] args) {
Demo d1 = new Demo(4); //创建新对象,并且赋值
System.out.println(d1); //
Demo d2 = new Demo(7);
System.out.println(d2.toString());
}
}
class Demo
{
private int num;
Demo(int num)
{
this.num = num;
}
public boolean equals(Object obj)
{
if(!(obj instanceof Demo))
return false;
Demo d = (Demo)obj;
return this.num == d.num;
}
public String toString()
{
return "demo:"+num;
}
}
class Person
{}
运行结果
demo:4
demo:7
问题,为什么System.out.println(d1); 打印不是一个地址值?
System.out.println(d2.toString()); 和System.out.println(d2);都一样..
|
|