20190222课堂作业<Map>

学生类

public class Student {
    private String name;
    private String sex;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

测试类

public class Test01 {
    public static void main(String[] args) {
        Map<String, Student> studentMap = new HashMap<String, Student>();
        Student stu1 = new Student();
        stu1.setName("李明");
        stu1.setSex("男");
        Student stu2 = new Student();
        stu2.setName("张三");
        stu2.setSex("男");
        Student stu3 = new Student();
        stu3.setName("王梅");
        stu3.setSex("女");

        studentMap.put("Jack", stu1);
        studentMap.put("Tom", stu2);
        studentMap.put("Rose", stu3);

        Student person = studentMap.get("Jack");
        System.out.println("Jack对应的学员姓名是:" + person.getName() + ":性别是:"
                + person.getSex());

    }
}

Console

Jack对应的学员姓名是:李明:性别是:男

猜你喜欢

转载自www.cnblogs.com/yanyu19/p/10419804.html