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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 蒋淑静 中级黑马   /  2013-12-9 19:05  /  1543 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.util.*;
  2. import java.io.*;
  3. //为了使其属性具备比较性要实现Comparable接口
  4. class Student implements Comparable
  5. {
  6.         private String name;
  7.         private int ma,cn,en;//ma:数学,cn:语文 en:英语
  8.                 private int sum;//sum:总成绩
  9.                 public Student(String name,int ma,int cn,int en){
  10.                         this.name=name;
  11.                         this.ma=ma;
  12.                         this.cn=cn;
  13.                         this.en=en;
  14.                         sum=ma+cn+en;
  15.                 }
  16.                 //覆盖compareTo方法:
  17.                 public int  compareTo(Object obj){
  18.                         Student s=(Student)obj;//强转
  19.                         int num=new Integer(this.sum).compareTo(new Integer(s.sum));//比较总分数
  20.                         if(num==0){
  21.                                 //如果总分数相等就比较姓名
  22.                                 return this.name.compareTo(s.name);
  23.                         }
  24.                         return num;
  25.                 }
  26.                 public int hashCode(){
  27.                         return name.hashCode()+sum*78;
  28.                 }
  29.                 public boolean equals(Object obj){
  30.                         if(!(obj instanceof Student)){
  31.                                 throw new ClassCastException("类型不匹配");       
  32.                         }
  33.                         Student s=(Student)obj;
  34.                         return this.name.equals(s.name)&&this.sum==s.sum;
  35.                 }
  36.                 public String toString(){
  37.                         return "String["+name+","+ma+","+cn+","+en+"]";
  38.                         }
  39.                 public String getName(){
  40.                         return name;
  41.                 }
  42.                 public int getMa(){
  43.                         return ma;
  44.                 }
  45.                 public int getCn(){
  46.                         return cn;
  47.                 }
  48.                 public int getEn(){
  49.                         return en;
  50.                 }
  51.                 public int getSum(){
  52.                         return sum;
  53.                 }
  54. }
  55. //定义工具类
  56. class StudentInfoTool
  57. {
  58.         public static Set<Student>getStudents()throws IOException{
  59.                 //键盘录入
  60.                 BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
  61.                 String line=null;
  62.                 //创建集合对象
  63.                 Set<Student> stus=new TreeSet<Student>();
  64.                 while((line=bufr.readLine())!=null){
  65.                         if("over".equals(line)){
  66.                                 break;
  67.                         }
  68.                         String []info=line.split(",");//以“,”来切割键盘输入的数据
  69.                         Student stu=new Student(info[0],Integer.parseInt(info[1]),Integer.parseInt(info[2]),Integer.parseInt(info[3]));
  70.                         stus.add(stu);//将对象存到集合
  71.                 }
  72.                 bufr.close();
  73.                 return stus;//返回集合
  74.         }
  75.         //将集合中的数据写到文件中
  76. public static void write2File(Set<Student> stus)throws IOException{
  77.         BufferedWriter bfs=new BufferedWriter(new FileWriter("d:\\123.txt"));
  78.         for(Student s:stus){
  79.                 bfs.write(s.getName());
  80.                 bfs.write(+s.getMa()+"");
  81.                 bfs.write(s.getCn()+"");
  82.                 bfs.write(s.getEn()+"");
  83.                 bfs.write(s.getSum()+"");
  84.                 bfs.newLine();
  85.                 bfs.flush();
  86.         }
  87.         bfs.close();
  88.         }
  89. }
  90. class  StudentInfoTest
  91. {
  92.         public static void main(String[] args) throws IOException
  93.         {
  94.                 Set<Student> stu=StudentInfoTool.getStudents();
  95.                 StudentInfoTool.write2File(stu);
  96.         }
  97. }
复制代码


这个代码我有三个地方不明白,Student类中为什么要覆盖hashCode和equals方法,干什么用?还有就是,覆盖的toString方法,怎么感觉没有调用呢,没按着toString的格式写入?

评分

参与人数 1技术分 +1 黑马币 +3 收起 理由
狼王 + 1 + 3 赞一个!

查看全部评分

7 个回复

倒序浏览
在Student类中覆盖hashCode和equals方法其实是为了在被调用时更加灵活,不仅可以用treeset还可以用hashset;toString方法是在StudentInfoTool中被用于存储至集合中,但是在输出的时候,用到的是write2File中的write方法,因此在打印出的文件上是看不到toString的效果的。

评分

参与人数 1技术分 +1 黑马币 +3 收起 理由
狼王 + 1 + 3 赞一个!

查看全部评分

回复 使用道具 举报
hashCode和equals还是不是很明白,他们里面的语句我不明白,还有如果我想按照toString的格式写入到文件中那代码怎么改呢
回复 使用道具 举报
以上程序没有太看懂,但我讲讲你的说的“为什么要覆盖hashCode和equals方法”。
在map集合中有一个HashMap集合,底层使用的是哈希表的方式存储。
    数据或对象在存入前先获取数据或对象的hash值,让后更具hash值顺序进行存储。
    当两个数据或者对象的hash值相同时候,再用equals判断两个数据或对象是不是同一个数据或对象。
所以我们在自定义数据或对象唯一性条件的时候需要改写这两个方法,使得集合满足我们自定义的需求。

评分

参与人数 1技术分 +1 黑马币 +3 收起 理由
狼王 + 1 + 3 赞一个!

查看全部评分

回复 使用道具 举报
那这两个方法也是自动调用的吗。更toString()是的在打印时会自动调用吗?

评分

参与人数 1技术分 +1 收起 理由
狼王 + 1 赞一个!

查看全部评分

回复 使用道具 举报
依然 发表于 2013-12-9 21:22
以上程序没有太看懂,但我讲讲你的说的“为什么要覆盖hashCode和equals方法”。
在map集合中有一个HashMap ...


那这两个方法也是自动调用的吗。更toString()是的在打印时会自动调用吗?
回复 使用道具 举报
蒋淑静 发表于 2013-12-9 20:01
hashCode和equals还是不是很明白,他们里面的语句我不明白,还有如果我想按照toString的格式写入到文件中那 ...

那两个方法是会自动调用的,看你要用什么集合来进行存储了,现在是treeset,只调用了compareTo方法。
若想按照toString的形式打印,不妨改下write2File方法中的如下代码:
  1. bfs.write(s.getName());
  2.                 bfs.write(":"+s.getMa()+",");
  3.                 bfs.write(s.getCn()+",");
  4.                 bfs.write(s.getEn()+",");
  5.                 bfs.write(s.getSum()+"");
  6.                 bfs.newLine();
  7.                 bfs.flush();
复制代码
回复 使用道具 举报
hashCode和equals方法我现在搞清楚了,可是我看毕老师的教程中没有这样写,而是按照toString中的格式写入文件,所以我才不明白,不知道你又没有看这集的视频教程
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马