package com;
import java.util.TreeSet;
public class Test {
public static void main(String[] args) {
/*
* 先变量,再构造,然后方法。但
*/
TreeSet set = new TreeSet();
set.add(new Child());
set.add(new Parent(3));
set.add(new Parent(4));
set.add(new Parent(5));
set.add(new Parent(6));
//System.out.println(set.size());
}
}
class Child extends Parent {
public Child(){
super(110);
}
public int compareTo(Object o) {
// TODO Auto-generated method stub
System.out.println("method of child");
// Child o1 = (Child)o;
return 1;
}
@Override
public String toString() {
return "age";
}
}
class Parent implements Comparable {
private int age = 0;
public Parent(int age){
this.age = age;
}
@Override //这里为什么用toString方法
public String toString() {
return "age";}
public int compareTo(Object o) {
System.out.println("method of parent");
Parent o1 = (Parent)o;
System.out.println("age:" + age + " o1.age:" + o1.age + " o对象:" + o );
return age>o1.age?1:age<o1.age?-1:0;
}
} |