equals重写的例子

来源:互联网 发布:2013年最火的网络歌曲 编辑:程序博客网 时间:2024/05/01 01:01


package lin.sxt;


class Cat
{
 private int color;
 private int hight;
 private int weight;
 public Cat(){}
 public Cat(int color,int hight,int weight)
 {
  this.color = color;
  this.hight = hight;
  this.weight = weight;
 }
 public boolean equals(Object obj)
 {
  if(obj == null)
   return false;
  else
  {
   if(obj instanceof Cat)
   {
    Cat c =(Cat)obj;
    if(c.color==this.color && c.hight==this.hight && c.weight==this.weight)
     return true;
   }
  }
  return false;
 }
}
public class TestEquale {

 public static void main(String[] args) {
  Cat a = new Cat();
  Cat b = new Cat();
  System.out.println(a.equals(b));
  Cat x = new Cat(1,2,3);
  Cat y = new Cat(1,2,3);
  System.out.println(x.equals(y));
  System.out.println(a.equals(x));
 }
}