gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:10:19 24 4
gpt4 key购买 nike

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com