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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 史政法 中级黑马   /  2013-3-1 13:27  /  2209 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 史政法 于 2013-3-1 21:01 编辑
  1. import java.util.*;
  2. class Student implements Comparable
  3. {
  4.         private String name;
  5.         private int age;
  6.         
  7.         public int compareTo(Object obj)
  8.         {
  9.                 if (!(obj instanceof Student))
  10.                 {
  11.                         throw new RuntimeException("No Student Class");
  12.                 }
  13.                 Student s = (Student)obj;

  14.                 if (this.age>s.age)//这里接收的一个Object类进来,并进行强转动作,为什么能直接调用私有成员s.age
  15.                 {
  16.                         return 1;
  17.                 }
  18.                 if (this.age==s.age)
  19.                 {
  20.                         return this.name.compareTo(s.name);
  21.                 }
  22.                 return -1;
  23.         }

  24.         public Student(String name ,int age)
  25.         {
  26.                 this.name = name;
  27.                 this.age = age;
  28.         }

  29.         public String getName()
  30.         {
  31.                 return name;
  32.         }
  33.         public int getAge()
  34.         {
  35.                 return age;
  36.         }
  37. }

  38. class MyComparator implements Comparator
  39. {
  40.         public int compare(Object o1,Object o2)
  41.         {
  42.                 if (!(o1 instanceof Student && o2 instanceof Student))
  43.                 {
  44.                         throw new RuntimeException("No Student...");
  45.                 }
  46.                 Student s = (Student)o1;
  47.                 Student s2 = (Student)o2;
  48.                 int num = s.name.compareTo(s2.name);//这里同样接收的是Objcet类,也做了强转动作,为什么不能使用私有成员。他们之间区别在哪里?内存中是怎样的?
  49.                 if (num ==0 )
  50.                 {
  51.                         new Integer(s.age).compareTo(new Integer(s2.age));
  52.                 }
  53.                 return num;
  54.         }
  55. }

  56. class  TreeSetDemo2
  57. {
  58.         public static void main(String[] args)
  59.         {
  60.                 TreeSet ts = new TreeSet(new MyComparator());
  61.                 ts.add(new Student ("abc",10));
  62.                 ts.add(new Student ("bac",11));
  63.                 ts.add(new Student ("cab",12));
  64.                 ts.add(new Student ("acb",13));

  65.                 for (Iterator it = ts.iterator();it.hasNext() ; )
  66.                 {
  67.                         sop(it.next());
  68.                 }
  69.         }
  70.         public void sop(Object obj)
  71.         {
  72.                 System.out.println(obj);
  73.         }
  74. }
复制代码
问题详情见代码。。。。。。

8 个回复

倒序浏览
私有成员只能在自己内部被访问。
if (this.age>s.age)//这里你是在Student自己的成员方法中,当然可以直接调用

而  int num = s.name.compareTo(s2.name);//此处你是在其它类中访问Student类中的私有属性,就不能直接访问到,
当你创建Student对象时在堆内存开辟空间,私有成员对外是关闭的,不能直接被访问,外部要想访问只能必须调用它给你提供的访问接口。个人见解
回复 使用道具 举报
  1. import java.util.*;

  2. class Student implements Comparable

  3. {

  4.         private String name;

  5.         private int age;

  6.         

  7.         public int compareTo(Object obj)

  8.         {

  9.                 if (!(obj instanceof Student))

  10.                 {

  11.                         throw new RuntimeException("No Student Class");

  12.                 }

  13.                 Student s = (Student)obj;



  14.                 if (this.age>s.age)

  15.                 {

  16.                         return 1;

  17.                 }

  18.                 if (this.age==s.age)

  19.                 {

  20.                         return this.name.compareTo(s.name);

  21.                 }

  22.                 return -1;

  23.         }



  24.         public Student(String name ,int age)

  25.         {

  26.                 this.name = name;

  27.                 this.age = age;

  28.         }

  29.                 public String getName()

  30.         {

  31.                 return name;

  32.         }




  33. }





  34. class  Test25

  35. {

  36.         public static void main(String[] args)

  37.         {

  38.                 TreeSet ts = new TreeSet();

  39.                 ts.add(new Student ("abc",10));

  40.                 ts.add(new Student ("bac",11));

  41.                 ts.add(new Student ("cab",12));

  42.                 ts.add(new Student ("acb",13));



  43.                 for (Iterator it = ts.iterator();it.hasNext() ; )

  44.                 {

  45.                        System.out.println(((Student)it.next()).getName());

  46.                 }

  47.         }

  48.         public void sop(Object obj)

  49.         {

  50.                 System.out.println(obj);

  51.         }

  52. }
复制代码
这个Comparato方法的确很奇怪,上面我把getAge()方法给删了,没有任何影响,说明内部不需要getAge()方法来比较。 我想它可能用了反射直接获取成员的值进行对比。
至于为什么要这么做,我想可能是因为,你不能保证每一个加入集合的对象都没有私有属性,也不能保证这些对象都有getX()方法,如果就因为要加入集合而定义这个方法有点麻烦,重复。这样的话不如直接用反射,你只要有成员,继承Comparable接口就可以比较。
而如你发现的那样,Comparator却没有这个功能,是啊,到底是为什么呢?

以上只是个人暂时的意见,不对之处请见谅。
回复 使用道具 举报
内部类要访问外部类的成员变量有两种方式,第一种外部类的成员变量有final来修饰;第二中在内部类里可以采用外部类名.this.外部类成员变量这样的方式来访问,不能直接访问,至于在内存中是什么情况,还请高手拆招
回复 使用道具 举报
本帖最后由 刘圣繁 于 2013-3-1 18:41 编辑
  1. import java.util.*;
  2. class Student implements Comparable
  3. {
  4. private String name;
  5. private int age;

  6. public int compareTo(Object obj)
  7. {
  8. if (!(obj instanceof Student))
  9. {
  10. throw new RuntimeException("No Student Class");
  11. }
  12. Student s = (Student)obj;

  13. if (this.age>s.age)/注意注意:::在本类中的语句当然可以用本类中的私有成员     本类中的私有成员是拒绝其他类的直接访问  但可以在本类中使用  好像黑盒子里面的东西  外面的人看不到 里面的人可以用 */
  14. {
  15. return 1;
  16. }
  17. if (this.age==s.age)
  18. {
  19. return this.name.compareTo(s.name);
  20. }
  21. return -1;
  22. }

  23. public Student(String name ,int age)
  24. {
  25. this.name = name;
  26. this.age = age;
  27. }

  28. public String getName()
  29. {
  30. return name;
  31. }
  32. public int getAge()
  33. {
  34. return age;
  35. }
  36. }

  37. class MyComparator implements Comparator
  38. {
  39. public int compare(Object o1,Object o2)
  40. {
  41. if (!(o1 instanceof Student && o2 instanceof Student))
  42. {
  43. throw new RuntimeException("No Student...");
  44. }
  45. Student s = (Student)o1;
  46. Student s2 = (Student)o2;
  47. int num = s.name.compareTo(s2.name);/*注意:在类MyComparator中不能直接使用类Student 的私有变量     因为私有了  外面的类就不能直接访问 想访问可以这样:int num=s.getName().compareTo(s2.getName()) 用类Student的方法访问Student的私有变量*/
  48. if (num ==0 )
  49. {
  50. new Integer(s.age).compareTo(new Integer(s2.age));
  51. }
  52. return num;
  53. }
  54. }

  55. class TreeSetDemo2
  56. {
  57. public static void main(String[] args)
  58. {
  59. TreeSet ts = new TreeSet(new MyComparator());
  60. ts.add(new Student ("abc",10));
  61. ts.add(new Student ("bac",11));
  62. ts.add(new Student ("cab",12));
  63. ts.add(new Student ("acb",13));

  64. for (Iterator it = ts.iterator();it.hasNext() ; )
  65. {
  66. sop(it.next());
  67. }
  68. }
  69. public void sop(Object obj)
  70. {
  71. System.out.println(obj);
  72. }

  73. }
  74. 私有的变量 可以在本类中被访问  但不可以在本类外被直接访问
复制代码

评分

参与人数 1技术分 +1 收起 理由
高境 + 1 神马都是浮云

查看全部评分

回复 使用道具 举报
楼上几位大神,我的错,应该把代码写简单点再问的,,,,,,什么内部类什么玩意的全出来了,真崩溃,有内部类吗?有反射吗?

结帖,简化一下代码重新开帖,,,,,
回复 使用道具 举报
史政法 发表于 2013-3-1 21:01
楼上几位大神,我的错,应该把代码写简单点再问的,,,,,,什么内部类什么玩意的全出来了,真崩溃,有内 ...

我是说他内部可能用了反射原理......
回复 使用道具 举报
刘圣繁 发表于 2013-3-1 18:37

这个最后的总结应该写成:私有的变量,可以在本类中被本类的对象引用访问,但不可以在本类外被直接访问。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马