- class MapDemo
- {
- public static void main(String[] args)
- {
- HashMap<Student,String> hm = new HashMap<Student,String>();
- hm.put(new Student("zhangsan1",21),"shanghai");
- hm.put(new Student("zhangsan2",22),"guangzhou");
- hm.put(new Student("lisi3",23),"nanjing");
- hm.put(new Student("lisi4",24),"wuhan");
-
- Set<Student> keySet = hm.keySet();
- Iterator<Student> it = keySet.iterator();
-
- while(it.hasNext())
- {
- Student stu = it.next();
- // String stu1 = stu.toString();
- //String name = stu.getName();
- //int age = stu.getAge();
- String add = hm.get(stu);
- System.out.println(stu+"..."+add);
- }
-
-
-
- }
- }
复制代码
输出结果:C:\Users\Administrator\Desktop
请问代码中为什么获得student对象stu后就可以直接打印出学生信息而不需要调用学生类里面的方法来输出打印学生名字和年龄呢? |
|