java - 如何将具有多个参数的对象的单个参数添加到该对象的数组列表中的字符串中?

标签 java eclipse string arraylist tostring

我正在尝试重写 toString() 以打印名称。我使用对象员工数组列表中每个员工的 getNm() 。员工有参数(工资、工作时间、工作时间、加类时间)
我已将我关注的代码加粗。

public class Main {
    public static void main(String[] args){
        workers workerlist = new workers();
        workerlist.setNumberEmployees();
        workerlist.instantiateEmployees();
        System.out.println(workerlist.toString());
    }
}

public class Employees extends workers{
    public double pay;
    public String nm;
    public double hours;
    public double overtime;

    public Employees(double pay, String nm, double hoursWorked, double overtimeHours){
    }

        public double getPay(){
            return pay;
        }

        public void setPay(double pay){
        }

        public String getNm(){
            return nm;
        }

        public void setNm(String nm){
        }

        public double getHours(){
            return hours;
        }

        public void setHours(double hours){
        }

        public double getOvertime(){
            return overtime;
        }

        public void setOvertime(double overtime){
        }

    }

  import java.util.ArrayList;
import java.util.Scanner;

public class workers{
    public int employeenumber;
    public String nm;
    ArrayList<Employees> workerList = new ArrayList<Employees>();
    Scanner input = new Scanner(System.in);

    public void setNumberEmployees(){
        System.out.println("How many employees do you have?");
        employeenumber = input.nextInt();
    }

    public int getNumberEmployees(){
        return employeenumber;
    }

    public void instantiateEmployees(){
        for(int i=1; i<=employeenumber; i++){
            workerList.add(new Employees(0.0, "nm", 0.0, 0.0));
        }
    }


    public String toString(){
        String st = "";
        for(int i=0; i<employeenumber; i++){
            **st += workerList.toString();**
// ".toString() is there to test the setting of parameters, I am interested in replacing this part.
        }

        return st;
    }

}

预期输出[员工 1 的姓名、员工 2 的姓名、...员工 n 的姓名]

最佳答案

我认为下面的代码将给出您预期的结果。

public class Main {
    public static void main(String[] args) {
        workers workerlist = new workers();
        workerlist.setNumberEmployees();
        workerlist.instantiateEmployees();
        System.out.println(workerlist.toString()); //call toString to take workerlist
    }
}

重写Employees toString方法并且不要忘记更新构造函数中的nm参数

public class Employees extends workers {
    public double pay;
    public String nm;
    public double hours;
    public double overtime;

    public Employees(double pay, String nm, double hoursWorked, double overtimeHours) {
        this.nm = nm; //do not forget set value
    }

    public double getPay() {
        return pay;
    }

    public void setPay(double pay) {
    }

    public String getNm() {
        return nm;
    }

    public void setNm(String nm) {
    }

    public double getHours() {
        return hours;
    }

    public void setHours(double hours) {
    }

    public double getOvertime() {
        return overtime;
    }

    public void setOvertime(double overtime) {
    }

    @Override
    public String toString() {
        return getNm(); //Employees toString method will return nm
    }

}

重写 toString 方法并调用 arraylist 的 toString 方法。

public class workers {
    public int employeenumber;
    public String nm;
    ArrayList<Employees> workerList = new ArrayList<Employees>();
    Scanner input = new Scanner(System.in);

    public void setNumberEmployees() {
        System.out.println("How many employees do you have?");
        employeenumber = input.nextInt();
    }

    public int getNumberEmployees() {
        return employeenumber;
    }

    public void instantiateEmployees() {
        for (int i = 1; i <= employeenumber; i++) {
            workerList.add(new Employees(0.0, "nm", 0.0, 0.0));
        }
    }

    public String toString() {
        return workerList.toString(); //no need to iterate on arraylist, Arraylist.toString method will call each Employees toString method.
    }
}

关于java - 如何将具有多个参数的对象的单个参数添加到该对象的数组列表中的字符串中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36007632/

相关文章:

java - 删除布局中按/触摸时的深色高光

java - 在 Eclipse 中显示图像图标不起作用

javascript - 将带有双引号和单引号的字符串从 asp 传递到 javascript 函数

java - 根据唯一值将列表拆分为子列表

java - 以编程方式更改启动 Activity 样式

c++ - 使用 Eclipse 创建动态加载的 Linux 库

java - 在 Eclipse E4 中设置固定零件尺寸

python - 计数在一个字符串中运行

javascript - 从对象数组创建字符串的最简单方法

Java util.Scanner 无法立即识别用户输入