- class Demo
- {
- 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 boolean compare(Demo d)
- {
- return this.num==d.num;
- }
- */
- public String toString()
- {
- return "demo:"+num; [color=Red]其实这里是模糊的,[/color]
- }
- }
- class Person
- {
- }
- public class objTostring
- {
- public static void main(String[] args)
- {
- Demo d1 = new Demo(4);
- System.out.println(d1);//输出语句打印对象时,会自动调用对象的toString方法。打印对象的字符串表现形式。
- Demo d2 = new Demo(7);
- System.out.println(d2.toString());
- }
- }
复制代码 代码运行的结果为:demo:4
demo:7 模糊了、怎么个执行的。 |