重写equals方法

package Week8;
class Bird extends Animals{
	int bird;
	Bird(int a,String name){
		this.bird = a;		
		this.Kinds=name;
	}
}
class Animals{
	String Kinds;
}
public class RWequals {
	public static void main(String[] args) {
		Bird b = new Bird(3,"鸟类");
		Animals k = b;
		b.equals(k);
		System.out.println(b.equals(k));
	}
	public boolean equals(Object obj) {
		if(obj instanceof Bird) {  //instanceof测试obj是否指向Bird类
			Bird b = (Bird) obj;
			if(this==obj) return true;
			if(this.equals(b.bird)) return true ;
			return false;
		}
		else return false;
	}
}

你可能感兴趣的:(重写equals方法)