package cn.itcast;
public class Student {
private String name;
private int age;
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;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "name:"+name+"age:"+age;
}
}
package cn.itcast;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class Map {
public static void main(String[] args) {
HashMap<Integer,Student> hm = new HashMap<Integer,Student>();
Student s1 = new Student();
s1.setName("张惠妹");
s1.setAge(20);
Student s2 = new Student();
s2.setName("张惠妹");
s2.setAge(20);
Student s3 = new Student();
s3.setName("张惠妹");
s3.setAge(20);
hm.put(1, s1);
hm.put(2, s2);
hm.put(3, s3);
System.out.println(hm);//我在Student类里重写了toString方法,为什么hm对象不用调用此方法,这个方法就直接应用了啊,不应该是(hm.toString)吗?这里不解啊 求高手相助!
}
}
|
|