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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 黄敏 于 2012-8-15 18:14 编辑

import java.util.*;


class Student{
        private String name;
        private int age;
        private int score;
        public Student(String name, int age){
                this.setName(name);
                this.setAge(age);
                //this.setScore(score);
        }
        public String getName() {
                return name;
        }
        public void setName(String name) {
                this.name = name;
        }
        public int getAge() {
                return age;
        }
        public void setAge(int age) {
                this.age = age;
        }
        public int getScore() {
                return score;
        }
        public void setScore(int score) {
                this.score = score;
        }
        
        public int compareTo(Object obj){
                if (!(obj instanceof Student))
                        throw new RuntimeException("这不是学生类");
                Student stu = (Student)obj;
                if (this.score > stu.score)
                return 1;
                if(this.score== stu.score){
                        this.name.compareTo(stu.name);
                }
                return -1;
        }

}

class MyComparator implements Comparator{
        
        public int compare(Object o1, Object o2){
                Student s1 = (Student)o1;
                Student s2 = (Student)o2;
                int n = new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
                if(n==0){
                        return new Integer(s1.getScore()).compareTo(new Integer(s2.getScore()));
                }
                return n;
        }
}

public class Test10 {

        public static void main(String[] args) {
                HashMap mp = new HashMap();
                mp.put(new Student("张三",17), 89);
                mp.put(new Student("李四",18), 90);
                mp.put(new Student("王五",17), 91);
               
                Set se = mp.keySet();
                Iterator it = se.iterator();
                while (it.hasNext()){
                        Student stu = (Student)it.next();
                        int val = (Integer) mp.get(stu);
                        System.out.println(stu +","+ val);//为什么打印出来的是地址值呢?,用 System.out.println(stu.getName()+", "+stu.getAge() +", "+ val);  打印的就是学生信息了呢     求解   

为什么视频教程里老师说的例子就没有toString方法  打印的就是内容呢?看视频 《黑马程序员_毕向东_Java基础视频教程第16天-06-集合(Map练习)》

                }               
        }
}


评分

参与人数 1技术分 +1 收起 理由
张立江 + 1 童鞋要注意细节哈,加油!

查看全部评分

6 个回复

倒序浏览
  1. import java.util.*;


  2. class Student{
  3.         private String name;
  4.         private int age;
  5.         private int score;
  6.         public Student(String name, int age){
  7.                 this.setName(name);
  8.                 this.setAge(age);
  9.                 //this.setScore(score);
  10.         }
  11.         public String getName() {
  12.                 return name;
  13.         }
  14.         public void setName(String name) {
  15.                 this.name = name;
  16.         }
  17.         public int getAge() {
  18.                 return age;
  19.         }
  20.         public void setAge(int age) {
  21.                 this.age = age;
  22.         }
  23.         public int getScore() {
  24.                 return score;
  25.         }
  26.         public void setScore(int score) {
  27.                 this.score = score;
  28.         }
  29.         
  30.         public int compareTo(Object obj){
  31.                 if (!(obj instanceof Student))
  32.                         throw new RuntimeException("这不是学生类");
  33.                 Student stu = (Student)obj;
  34.                 if (this.score > stu.score)
  35.                 return 1;
  36.                 if(this.score== stu.score){
  37.                         this.name.compareTo(stu.name);
  38.                 }
  39.                 return -1;
  40.         }

  41. }

  42. class MyComparator implements Comparator{
  43.         
  44.         public int compare(Object o1, Object o2){
  45.                 Student s1 = (Student)o1;
  46.                 Student s2 = (Student)o2;
  47.                 int n = new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
  48.                 if(n==0){
  49.                         return new Integer(s1.getScore()).compareTo(new Integer(s2.getScore()));
  50.                 }
  51.                 return n;
  52.         }
  53. }

  54. public class Test10 {

  55.         public static void main(String[] args) {
  56.                 HashMap mp = new HashMap();
  57.                 mp.put(new Student("张三",17), 89);
  58.                 mp.put(new Student("李四",18), 90);
  59.                 mp.put(new Student("王五",17), 91);
  60.                
  61.                 Set se = mp.keySet();
  62.                 Iterator it = se.iterator();
  63.                 while (it.hasNext()){
  64.                         Student stu = (Student)it.next();
  65.                         int val = (Integer) mp.get(stu);
  66.                         System.out.println(stu +","+ val);//因为你直接将对象输出是调用对象的tostring()方法,而默认的tostring方法输出为类名@对象的16进制Hash表示;
  67.                        当你改用System.out.println(stu.getName()+", "+stu.getAge() +", "+ val);是分别取出对象中属性的值打印输出;
  68.                        如果你想直接打印输出对象必须重写student类的tostring方法。
  69.                 }               
  70.         }
  71. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
张立江 + 1 很给力!

查看全部评分

回复 使用道具 举报
打印语句会调用本身的toString()方法,如果没有覆盖toString()方法,就会调用Object类的toString()方法,而Object类的toString()方法打印的就是地址值。
本例中打印stu的时候,会找Student类中是重写了toString()方法,结果没有,那么默认调用了Object类的toString()方法,打印了对象的地址值。
如果在Student类中重写toString()方法r如:
public String toString()
{
        return name+"::"+age;
}
则会打印出名称和年龄信息

评分

参与人数 1技术分 +1 收起 理由
张立江 + 1 很给力!

查看全部评分

回复 使用道具 举报
周瑞 发表于 2012-8-15 17:56

为什么视频教程里老师说的例子就没有toString方法  打印的就是内容呢?看视频 《黑马程序员_毕向东_Java基础视频教程第16天-06-集合(Map练习)》
回复 使用道具 举报
王少岩 发表于 2012-8-15 18:00
打印语句会调用本身的toString()方法,如果没有覆盖toString()方法,就会调用Object类的toString()方法,而 ...

为什么视频教程里老师说的例子就没有toString方法  打印的就是内容呢?看视频 《黑马程序员_毕向东_Java基础视频教程第16天-06-集合(Map练习)》
回复 使用道具 举报
黄敏 发表于 2012-8-15 18:07
为什么视频教程里老师说的例子就没有toString方法  打印的就是内容呢?看视频 《黑马程序员_毕向东_Java ...

看仔细,是有toString()方法的。。。如图:

aaaa.jpg (5.12 KB, 下载次数: 17)

aaaa.jpg
回复 使用道具 举报
黄敏 中级黑马 2012-8-15 18:13:19
7#
王少岩 发表于 2012-8-15 18:11
看仔细,是有toString()方法的。。。如图:

谢谢了啊,我太粗心了。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马