- class Demo //extends Object
- {
- 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;
- }
- }
- class ObjectDemo
- {
- public static void main(String[] args)
- {
- Demo p1=new Demo(5);
- Demo p2=new Demo(6);
- System.out.println(p1.equals(p2));
- }
- }
复制代码 这样运行很明显是false 但是我把 Demo d=(Demo) obj 去掉,再把下面改成 return this.num==num;
为什么结果就变成ture 了呢? |