import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
class Employee{
private String id;
private String name;
private double salary=2000;
public void setSalary(double salary){
this.salary=salary;
}
public double getSalary(){
return salary;
}
public void setID(String id){
this.id=id;
}
public void setName(String name){
this.name=name;
}
public String getID(){
return id;
}
public String getName(){
return name;
}
public Employee(String id,String name){
this.id=id;
this.name=name;
}
public String show(){
String sal=String.valueOf(salary);
return name+"\t"+"员工号"+id+"\t"+"工资"+sal;
}
}
public class play32 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee e1=new Employee("001","11tom");
Employee e2=new Employee("002","22tom");
Employee e3=new Employee("003","33tom");
Employee e4=new Employee("004","44tom");
Map employee=new HashMap();
employee.put("0001", e1);
employee.put("0002", e2);
employee.put("0003", e3);
employee.put("0004", e4);
//显示员工号为0004员工的信息
System.out.println( ((Employee) employee.get("0004")).show());
//显示所有员工的信息
System.out.println("所有员工的信息如下");
Iterator it=employee.keySet().iterator();
while(it.hasNext()){
System.out.println(((Employee) employee.get(it.next())).show());
//修改员工号为0002的工资为3000
System.out.println("修改员工信息");
Employee employees=(Employee) employee.get("0002");
employees.setSalary(3000);
System.out.println(((Employee)employee).show());
employee.remove("0003");
System.out.println("删除员工后,剩余员工的信息");
it=employee.keySet().iterator();
while(it.hasNext()){
System.out.println(((Employee)employee.get(it.next())).show());
}
}
}
import java.util.Iterator;
import java.util.Map;
class Employee{
private String id;
private String name;
private double salary=2000;
public void setSalary(double salary){
this.salary=salary;
}
public double getSalary(){
return salary;
}
public void setID(String id){
this.id=id;
}
public void setName(String name){
this.name=name;
}
public String getID(){
return id;
}
public String getName(){
return name;
}
public Employee(String id,String name){
this.id=id;
this.name=name;
}
public String show(){
String sal=String.valueOf(salary);
return name+"\t"+"员工号"+id+"\t"+"工资"+sal;
}
}
public class play32 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee e1=new Employee("001","11tom");
Employee e2=new Employee("002","22tom");
Employee e3=new Employee("003","33tom");
Employee e4=new Employee("004","44tom");
Map employee=new HashMap();
employee.put("0001", e1);
employee.put("0002", e2);
employee.put("0003", e3);
employee.put("0004", e4);
//显示员工号为0004员工的信息
System.out.println( ((Employee) employee.get("0004")).show());
//显示所有员工的信息
System.out.println("所有员工的信息如下");
Iterator it=employee.keySet().iterator();
while(it.hasNext()){
System.out.println(((Employee) employee.get(it.next())).show());
//修改员工号为0002的工资为3000
System.out.println("修改员工信息");
Employee employees=(Employee) employee.get("0002");
employees.setSalary(3000);
System.out.println(((Employee)employee).show());
employee.remove("0003");
System.out.println("删除员工后,剩余员工的信息");
it=employee.keySet().iterator();
while(it.hasNext()){
System.out.println(((Employee)employee.get(it.next())).show());
}
}
}
}
红色显错误Exception in thread "main" java.lang.ClassCastException: java.util.HashMap cannot be cast to first.Employee
at first.play32.main(play32.java:78)
2946

被折叠的 条评论
为什么被折叠?



