A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© cody 中级黑马   /  2015-5-18 22:39  /  473 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.util.*;
  2. class TreeSetDemo
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 TreeSet a1=new TreeSet();
  7.                 a1.add(new Student("lisi01",20));
  8.                 a1.add(new Student("lisi03",16));
  9.                 a1.add(new Student("lisi02",24));
  10.                 a1.add(new Student("lisi02",24));
  11.                 a1.add(new Student("lisi04",19));
  12.                 a1.add(new Student("lisi05",19));
  13.                 Iterator it=a1.iterator();
  14.                 while(it.hasNext())
  15.                 {       
  16.                         Student stu=(Student)it.next();
  17.                         System.out.println(stu.getName()+"......"+stu.getAge());
  18.                 }
  19.         }
  20. }
  21. class Student implements Comparable
  22. {
  23.         private String name;
  24.         private int age;
  25.         Student(String name,int age)
  26.         {
  27.                 this.name=name;
  28.                 this.age=age;
  29.         }
  30.         public String getName()
  31.         {
  32.                 return name;
  33.         }
  34.         public int getAge()
  35.         {
  36.                 return age;
  37.         }
  38.         public int compareTo(Object obj)
  39.         {
  40.                 if(!(obj instanceof Student))
  41.                         throw new RuntimeException("不是学生对象");
  42.                 Student s=(Student)obj;
  43.                 System.out.println(this.name+"...compareto..."+s.name);
  44.                 if(this.age>s.age)
  45.                         return 1;
  46.                 if(this.age==s.age)
  47.                 {
  48.                         return this.name.compareTo(s.name);
  49.                 }
  50.                 return -1;
  51.         }
  52. }
复制代码


System.out.println(this.name+"...compareto..."+s.name);
搞不清this.name和s.name的关系,求讲解?

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马