本帖最后由 伍碧林 于 2011-10-11 20:39 编辑
啊,这么多回复了啊...我没有去打球还是给你看了下,自己辛苦了下,还是把代码贴进来.修改的地方,注释中写了....{:soso_e100:}
结果:
刘一锋
性别:男 生日:22 学位 :22 标题:2 地址:100 电话:22(乱写的数据)- import java.util.*;
- class Employee {
- private String name;
- private char sex;
- private int birthday;
- private String degree;
- private String title;
- private String address;
- private String telephone;
- public Employee(char sex, int birthday, String degree, String title,String address,//改了
- String telephone) {
- this.sex = sex;
- this.birthday = birthday;
- this.degree = degree;
- this.title = title;
- this.address = address;
- this.telephone = telephone;
- }
- public Employee(String name)
- {
- this.name = name;
- }
- public void setName(String name)
- {
- this.name = name;
- }
- public void setSex(char sex)
- {
- this.sex = sex;
- }
- public void setBirthday(int birthday) {
- this.birthday = birthday;
- }
- public void setDegree(String degree)
- {
- this.degree = degree;
- }
- public void setTitle(String title)
- {
- this.title = title;
- }
- public void setAddress(String address, String title)
- {
- this.title = title;
- }
- public void setTelephone(String telephone)
- {
- this.telephone = telephone;
- }
- public String getName()
- {
- return name;
- }
- public char getSex()
- {
- return sex;
- }
- public int getBirthday()
- {
- return birthday;
- }
- public String getDegree()
- {
- return degree;
- }
- public String getTitle()
- {
- return title;
- }
- public String getAddress()
- {
- return address;
- }
- public String getTelephone()
- {
- return telephone;
- }
- //添加了
- public String toString(){
- return "性别:"+this.sex+" 生日:"+this.birthday+" 学位 :"+this.degree
- +" 标题:"+this.title+" 地址:"+this.address+ " 电话:"+this.telephone;
-
- }
- }
- class EmployManager
- {
- private static HashMap<String, Object> EmployeeListHT = new HashMap<String, Object>();
- // public Object getName(Object obj)
- // {
- // obj= get(obj);
- // return obj;
- //
- public boolean putEmployee(String name, Object obj)//
- {
- EmployeeListHT.put(name, obj);
- return true;
- }
- public void putEmployee(Employee employee, Employee employee2) {
- // TODO Auto-generated method stub
- }
- public void putEmployee(Object object, Object object2) {
- // TODO Auto-generated method stub
-
- }
- public static Employee getEmployee(String name)
- {
- return (Employee) EmployeeListHT.get(name);
- }
- //自动生成的代码
- //EmployeeListHT这个属性的get()和set()方法,因为后面用到了em.getEmployeeListHT(),因为你的EmployeeListHT 是private的,需要get()和set(),也只有它才是真正存数据的HashMap.
- public void setEmployeeListHT(HashMap<String, Object> employeeListHT) {
- EmployeeListHT = employeeListHT;
- }
- public HashMap<String, Object> getEmployeeListHT() {
- return EmployeeListHT;
- }
- }
- /*
- * public static boolean removeEmployee(String name)
- *
- * {
- *
- * EmployeeListHT.remove(name);
- *
- * return true;
- *
- * }
- */
- public class temp extends EmployManager
- {
- // public static void sop(Object obj)
- // {System.out.println(obj);}
- @SuppressWarnings("unchecked")
- public static void main(String[] srgs)
- {
- EmployManager em = new EmployManager();
- // em.putEmployee((Object) new Employee("刘一锋"), (Object) new Employee('1',
- // 22, "22", "2", "22"));
- //这里改了.根据你的意思.是通过"刘一锋"找他的信息
- //你用的putEmployee(Object object ,Object object2)这个方法,但是你方法体为空.所以就给你改成了
- //public boolean putEmployee(String name, Object obj)因为你有方法体
- em.putEmployee(new String("刘一锋"), (Employee) new Employee('男',
- 22, "22", "2","100", "22"));
- //你后面代码大概是这个意思...
- Set<Map.Entry<String, Object>> itor =em.getEmployeeListHT().entrySet();
- Iterator<Map.Entry<String, Object>> iter=itor.iterator();
- while(iter.hasNext()){
- Map.Entry<String, Object> me = iter.next();
- System.out.println(me.getKey());
- System.out.println(me.getValue());
- }
- // sop(toString());
- }
- }
复制代码 |