如:
public class Test
{
    Point p1=new Point(1,1);
    Point p2=new Point(1,1);
    问题:
     如何才能判断p1与p2是否相等
     怎么做才能让p1等于p2呢?
    
}
class Point
{
    int x,y;
    public Point(int x,int y)
    {
      this.x=x;
      this.y=y;
    }
}

解决方案 »

  1.   

    如果用==的话肯定不 等,用equals的话 ,重写下方法
      

  2.   

     如何才能判断p1与p2是否相等 对象的相等用p1.equals(p2)判断 怎么做才能让p1等于p2呢? 
    p1=p2
    用这样后,他们的引用是相同的
      

  3.   

    我用==和equals两种方法都试过了,结果都是不相等啊
      

  4.   


    public class Test 

    public static void main (String[] args) 
    {
        Point p1=new Point(1,1); 
        Point p2=new Point(1,1); 
        //问题: 
        //如何才能判断p1与p2是否相等 
        //怎么做才能让p1等于p2呢?  System.out.println(p1.equals(p2));

    //实际上p1无法等于p2,再像的双胞胎也还是不同的两个人
    //对象相等了就是同一个对象的引用了
    }
        

    class Point 

        int x,y; 
        public Point(int x,int y) 
        { 
          this.x=x; 
          this.y=y; 
        }
        
        public int getX()
        {
         return x;
        } 
        public int getY()
        {
         return y;
        } 
        
      public boolean  equals(Object o)
         {
         if(((Point)o).getX()==x&&((Point)o).getY()==y)
         {
         return true;
         }
         else return false;
         }
    }
      

  5.   

    另外可以用对象Clone,要实现Clonable接口
      

  6.   

    实现public boolean equals(Object obj) {
    if (obj == this) return true;
    if (obj == null) return false;
    if (obj.getClass() != this.getClass()) return false;
    Point other = (Point) obj;
    return other.x == this.x && other.y == this.y;
    }使用p1.equals(p2);