Java集合练习2-8, 烦请老师检查并指正~
public class Employee{
//根据需求完成Employee类的定义
private int id;
private String name;
private double salary;
public Employee(){
}
public Employee(int id, String name, double salary){
this.setId(id);
this.setName(name);
this.setSalary(salary);
}
public void setId(int id){
this.id = id;
}
public int getId(){
return this.id;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void setSalary(double salary){
this.salary = salary;
}
public double getSalary(){
return this.salary;
}
}
import java.util.List;
import java.util.ArrayList;
public class EmployeeTest {
public static void main(String[] args) {
//定义ArrayList对象
ArrayList list = new ArrayList();
//创建三个Employee类的对象
Employee emp1 = new Employee(1, "ZhangSan", 5000.0);
Employee emp2 = new Employee(2, "LiSi", 5500.0);
Employee emp3 = new Employee(3, "ZhaoLiu", 4000.0);
//添加员工信息到ArrayList中
list.add(emp1);
list.add(emp2);
list.add(emp3);
//显示员工的姓名和薪资
System.out.println("Employee's name and Employee's salary");
for(int i=0; i <list.size(); i++){
System.out.println(((Employee)(list.get(i))).getName() + " " + ((Employee)(list.get(i))).getSalary());
}
}
}
21
收起
正在回答
1回答
同学你好,测试代码符合题目要求,很棒呐,继续加油
祝学习愉快~
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星