- import java.util.*;
- class Student //implements Comparable
- {
- private String name;
- private int age;
- Student(String name,int age)
- {
- this.name=name;
- this.age=age;
- }
- public String getName()
- {
- return name;
- }
- public int getAge()
- {
- return age;
- }
- /*
- public int compareTo(Object obj)
- {
- if (!(obj instanceof Student))
- throw new RuntimeException();
- Student s=(Student)obj;
- if(this.age>s.age)
- return 1;
- if(this.age==s.age)
- {
- return this.name.compareTo (s.name);
- };
- return -1;
- }
- */
- }
- class Day1102
- {
- public static void main(String[] args)
- {
- TreeSet ts=new TreeSet();
- System.out.println(ts);
- ts.add(new Student("zhangsan01",11));
- //ts.add(new Student("zhangsan02",12));
- //ts.add(new Student("zhangsan03",13));
- //ts.add(new Student("zhangsan04",11));
- Iterator it=ts.iterator();
- while (it.hasNext())
- {
- Student s=(Student)it.next();
- System.out.println(s.getName()+"----"+s.getAge());
- }
-
-
- System.out.println("Hello World!");
- }
- }
复制代码 在毕老师的视频里面,未复写compareTo时,add的第一个对象是可以打印出来的,而我这个却不能。
要是把注释里面的内容释放出来,就可以了。
这是为什么呢?
|
|