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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© spiderman 中级黑马   /  2013-11-8 17:36  /  1219 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

以下是一个可以正确执行的代码
  1. import java.util.*;
  2. import java.io.*;

  3. class Student implements Comparable<Student>
  4. {
  5.         private String name;
  6.         private int en,cn,ma;
  7.         private int sum;

  8.         Student(String name,int en,int cn,int ma)
  9.         {
  10.                 this.name=name;
  11.                 this.en=en;
  12.                 this.cn=cn;
  13.                 this.ma=ma;       
  14.                 sum=en+cn+ma;
  15.         }

  16.         public String getSum()
  17.         {
  18.                 return this.name;
  19.         }



  20.         public int compareTo(Student stu)
  21.         {
  22.                 if(this.sum==stu.sum)//为什么可以stu.sum而不报错?sum不是私有变量吗?why?
  23.                         return this.name.compareTo(stu.name);
  24.                 return this.sum-stu.sum;
  25.         }

  26.         public int hashCode()
  27.         {
  28.                 return name.hashCode()+sum*36;
  29.         }

  30.         public boolean equals(Object obj)
  31.         {
  32.                 if(obj instanceof Student)
  33.                         return false;
  34.                 Student s=(Student)obj;
  35.                
  36.                 return s.name.equals(this.name) && s.sum==this.sum;
  37.         }

  38.         public String toString()
  39.         {
  40.                 return "student"+"["+name+","+ma+","+en+","+cn+","+sum+"]";
  41.         }
  42. }


  43. class StudentTool
  44. {
  45.         public static Set<Student> getStudents() throws IOException
  46.         {
  47.                 Set<Student> set=new TreeSet<Student>();       
  48.                 BufferedReader bufr=
  49.                         new BufferedReader(new InputStreamReader(System.in));
  50.                 String line=null;

  51.                 while((line=bufr.readLine())!=null)
  52.                 {
  53.                         if("over".equals(line))
  54.                                 break;
  55.                         String[] info=line.split(",");
  56.                        
  57.                         Student s=new Student(info[0],Integer.parseInt(info[1]),Integer.parseInt(info[2]),Integer.parseInt(info[3]));
  58.                        
  59.                         set.add(s);
  60.                 }
  61.                
  62.                 bufr.close();

  63.                 return set;
  64.         }

  65.         public static void write2File(Set<Student> stu) throws IOException
  66.         {
  67.                 BufferedWriter bw=new BufferedWriter(new FileWriter("student.txt"));
  68.                 for(Student s : stu)
  69.                 {
  70.                         bw.write(s.toString()+"\t");
  71.                         bw.write(s.getSum());
  72.                         bw.newLine();
  73.                         bw.flush();
  74.                 }
  75.                 bw.close();
  76.         }
  77. }

  78. class StudentsIOTest
  79. {
  80.         public static void main(String[] args) throws IOException
  81.         {
  82.                 Set<Student> ts=StudentTool.getStudents();
  83.                 StudentTool.write2File(ts);
  84.         }
  85. }
复制代码
正如代码里我注释的那样
  1. public int compareTo(Student stu)
  2.         {
  3.                 if(this.sum==stu.sum)//为什么可以stu.sum而不报错?sum不是私有变量吗?why?
  4.                         return this.name.compareTo(stu.name);//这个也是
  5.                 return this.sum-stu.sum;//这个也是!!
  6.         }
复制代码
为什么没报错?在这列为什么对象可以直接调用私有变量?!!百思不得其解啊!!!

评分

参与人数 1技术分 +1 收起 理由
乔兵 + 1

查看全部评分

2 个回复

倒序浏览
私有,对类外部访问有效,在该类内部访问无限制的,stu是该类对象,也在该类内部访问成员变量,没有问题,如果是在该类外部则不可。例如你定义比较器,都必须用定义的get方法

评分

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

查看全部评分

回复 使用道具 举报
寻丶丶觅 发表于 2013-11-8 18:11
私有,对类外部访问有效,在该类内部访问无限制的,stu是该类对象,也在该类内部访问成员变量,没有问题, ...

噢。。。是这样的。。。明白了,谢谢哈
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马