2-4作业
来源:2-4 编程练习
慕数据3515280
2023-03-10 11:45:14
import java.util.List; import java.util.ArrayList; public class EmployeeTest { public static void main(String[] args) { //定义ArrayList对象 ArrayList<Employee> employeeList = new ArrayList<Employee>(); //创建三个Employee类的对象 Employee e1 = new Employee(1,"张三",5000.0); Employee e2 = new Employee(1,"李四",5500.0); Employee e3 = new Employee(1,"赵六",4000.0); //添加员工信息到ArrayList中 employeeList.add(e1); employeeList.add(e2); employeeList.add(e3); //显示员工的姓名和薪资 System.out.println("员工姓名\t员工薪资"); for(Employee i:employeeList){ System.out.println(i.getName()+"\t\t"+i.getSalary()); } // System.out.println(employeeList.get(0).getName()+"\t\t"+employeeList.get(0).getSalary()); // System.out.println(employeeList.get(1).getName()+"\t\t"+employeeList.get(1).getSalary()); // System.out.println(employeeList.get(2).getName()+"\t\t"+employeeList.get(1).getSalary()); } }
public class Employee{ //根据需求完成Employee类的定义 private int id; private String name; private double salary; public Employee(){ } public Employee(int id, String name, double salary){ this.id = id; this.name= name; this.salary = salary; } public int getId(){ return id; } public void setId(int id){ this.id = id; } public String getName(){ return name; } public void setName(String name){ this.name = name; } public double getSalary(){ return salary; } public void setSalary(double salary){ this.salary = salary; } }
1回答
好帮手慕小蓝
2023-03-10
同学你好,同学的代码符合题目要求,逻辑清晰,书写规范,做的很棒。
祝学习愉快~
相似问题