public int compareTo(Cat c)
{
int num = this.name.compareTo(c.name);
if(num ==0)
return new Integer(age).compareTo(c.age);
return num;
}
}
class Dog extends Animal
{
Dog(String name,int age)
{
super(name,age);
}
public int hashCode()
{
return name.hashCode()+age*6;
}
public boolean equals(Object obj)
{
if(!(obj instanceof Dog))
throw new RuntimeException("不是dog对象");
Dog dog = (Dog) obj;
return this.name.equals(dog.name) && this.age == dog.age;
}
}
class Comp implements Comparator<Animal>//TreeSet集合比较器
{
public int compare(Animal a,Animal a2)
{
int num = new Integer(a.getAge()).compareTo(new Integer(a2.getAge()));
if(num ==0)
return a.getName().compareTo(a2.getName());
return num;
}
}
class AnimalSetTest
{
public static void main(String[] args)
{
HashSet<Cat> hs = new HashSet<Cat>();//HashSet集合元素无序,元素不可以重复,根据hashCode排序