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

How do you add a single parameter of an object with multiple parameters to a string from an arraylist of that object?

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

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 String toString(){
    String st = "";
    for(int i=0; i < employeenumber; i++)
        st += workerList.get(i).toString();

    return st;
}

但这个解决方案并不是最好的。我建议你下一个:

public String toString(){
    StringBuilder builder = new StringBuilder();
    workerList.stream().map(Employees::toString).forEach(builder::append);

    return builder.toString();
}

当然,您必须在 Employees class 中覆盖 toString()。例如,

@Override public String toString() {
    return new StringJoiner(", ")
                .add(Double.toString(pay))
                .add(nm)
                .add(Double.toString(hours))
                .add(Double.toString(overtime))
            .toString();
}

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

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.
    }
}

Yusuf K. 和 Andrew Tobilko 正确回答了您的问题。只是给你一些提示来改进你的 classes:

  • 尽管您的 class 可以代表一个员工,但您为什么称呼 Employees;所以我会称它为 Employee
  • 为什么要定义两次nm场?它在 Workers class 中声明,Employees class 从中派生。
  • setNumberEmployees() 方法不符合 JavaBeans 约定;它必须声明为

    public void setNumberEmployees(final String noEmployees) {
        employeenumber = noEmployees;
    }
    

    并且对 setNumberEmployees() 的调用应该在 main() 方法中完成

    public static void main(String[] args){
        workers workerlist = new workers();
        Scanner input = new Scanner(System.in);
        workerlist.setNumberEmployees(input.nextInt());
        workerlist.instantiateEmployees();
        System.out.println(workerlist.toString());
    }
    
  • 最后,你说的是参数。它们实际上被称为 fieldsinstance variable(s).