请老师检查
来源:2-4 编程练习
PhoebeJ
2022-08-10 22:16:33
public class Employee{ //根据需求完成Employee类的定义 private String name; private double salary; private int num; public Employee(){ } public Employee(String name, double salary){ this.setName(name); this.setSalary(salary); } public void setName(String name){ this.name = name; } public String getName(){ return name; } public void setSalary(double salary){ this.salary = salary; } public double getSalary(){ return salary; } public void setNum(int num){ this.num = num; } public int getNum(){ return num; } public String info(){ String str = "\n"+this.getName()+" "+this.getSalary(); return str; } }
import java.util.List; import java.util.ArrayList; public class EmployeeTest { public static void main(String[] args) { //定义ArrayList对象 ArrayList<Employee> emplist = new ArrayList<Employee>(); //创建三个Employee类的对象 Employee emp = new Employee("张三",5000.0); Employee emp1 = new Employee("李四",5500.0); Employee emp2 = new Employee("赵六",4000.0); //添加员工信息到ArrayList中 emplist.add(emp); emplist.add(emp1); emplist.add(emp2); //显示员工的姓名和薪资 System.out.print("员工姓名 员工薪资"); for(int i = 0;i < emplist.size();i++){ String str = emplist.get(i).info(); System.out.print(str); } } }
1回答
好帮手慕小黑
2022-08-11
同学你好,
建议同学在Employee中定义满参的构造方法。
同学代码运行效果符合编程练习要求。
祝学习愉快!
相似问题