老师请检查!
来源:2-4 编程练习
和你一起终身学习
2022-07-07 22:37:51
package com.imooc.collection.list;
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 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;
}
}package com.imooc.collection.list;
import java.util.ArrayList;
public class EmployeeTest {
public static void main(String[] args) {
//定义ArrayList对象
ArrayList<Employee> empList = new ArrayList<Employee>();
//创建三个Employee类的对象
Employee emp1 = new Employee(101, "张三", 5000);
Employee emp2 = new Employee(102, "李四", 5500);
Employee emp3 = new Employee(103, "赵六", 4000);
//添加员工信息到ArrayList中
empList.add(emp1);
empList.add(emp2);
empList.add(emp3);
//显示员工的姓名和薪资
System.out.println(empList.size());
System.out.println("员工姓名"+" "+"员工薪资");
System.out.print(" "+empList.get(0).getName()+" "+empList.get(0).getSalary());
System.out.println();
System.out.print(" "+empList.get(1).getName()+" "+empList.get(1).getSalary());
System.out.println();
System.out.print(" "+empList.get(2).getName()+" "+empList.get(2).getSalary());
}
}
1回答
好帮手慕小黑
2022-07-08
同学你好,程序运行结果符合题目要求,逻辑结构清晰,同学棒棒哒!
祝学习愉快!
相似问题