标签:
public class Demo{ public static void main(String[] args){ Animal a1 = new Tiger(); Animal a2 = new Fish(); Animal a3 = new Tiger(); a1.name = "aaa"; a1.age = 12; a1.type ="tiger"; a2.name = "aaa"; a2.age = 12; a2.type ="tiger"; a3.name = "aaa"; a3.age = 5; a3.type ="tiger"; System.out.println(a1.equals(a2)); System.out.println(a3.equals(a2)); } } class Animal{ String name; int age; String type; public boolean equals(Object obj){ boolean bool = false; if(this == obj){bool = true;} if(obj == null){bool = false;} if(obj instanceof Animal){ bool = ((Animal)obj).name==this.name &&((Animal)obj).age==this.age &&((Animal)obj).type==this.type; } return bool; } } class Tiger extends Animal{} class Fish extends Animal{}
标签:
原文地址:http://www.cnblogs.com/Harry369/p/5452504.html