gpt4 book ai didi

java - 为什么子类运行父类(super class)构造函数?

转载 作者:行者123 更新时间:2023-11-29 06:32:42 26 4
gpt4 key购买 nike

当我运行这段代码时,我只是想用轮类详细信息和员工填充一个列表,我在父类(super class)中预制了一个员工生成器,所以我认为最好不要重复代码。我能够创建类次数组列表并填充它,但每次我创建类次对象时它都会显示 EmployeeSheet 字段。

这是 EmployeeSheet 类:

import java.util.*;

public class EmployeeSheet
extends Employee {
private ArrayList<Employee> empSheet;
private ProductionWorker pworker;
private ShiftSupervisor sworker;

/**
* Constructs a list of randomly selected employees and displays the list.
*/
public EmployeeSheet() {
empSheet = new ArrayList<Employee>();

Random empPicker = new Random();

for (int i = 0; i < 20; i++) {
int id = empPicker.nextInt(20);
if (id < 12) // roll for production worker
{
System.out.println("Adding Production Worker");
empSheet.add(generateProductionWorker());
} else //roll for Shift supervisor
{
System.out.println("Adding Shift supervisor");
empSheet.add(generateShiftSupervisor());
}
}
Iterator iterator = empSheet.iterator();
while (iterator.hasNext()) {
System.out.println("");
System.out.println(iterator.next());

}
}

/**
* Generates a Production Worker
* @return The fields that make up a Production worker
*/

public ProductionWorker generateProductionWorker() {
Random rng = new Random();
int numberOfEmployeeNames = Ename.length;
ProductionWorker tempPworker = new ProductionWorker();

String employeeName = Ename[rng.nextInt(numberOfEmployeeNames)];
tempPworker.setEmployeeName(employeeName);

int numberOfEmployeeNumbers = Empnum.length;
String employeeNumber = Empnum[rng.nextInt(numberOfEmployeeNumbers)];
tempPworker.setEmployeeNumber(employeeNumber);

int yearHired = rng.nextInt(35) + 1980;
tempPworker.setEmployeehireyear(yearHired);

double weeklySalary = rng.nextInt((100) * 100);
tempPworker.setEmployeeweeklyearning(weeklySalary);

int hourlyRate = rng.nextInt(20) + 10;
tempPworker.setHourlyRate(hourlyRate);

pworker = tempPworker;

return tempPworker;

}

/**
* Generates a ShiftSupervisor employee
* @return the Fields that make up a shift supervisor
*/

public ShiftSupervisor generateShiftSupervisor() {
Random ran = new Random();
int numberOfEmployeeNames = Ename.length;
ShiftSupervisor tempSworker = new ShiftSupervisor();

String employeeName = Ename[ran.nextInt(numberOfEmployeeNames)];
tempSworker.setEmployeeName(employeeName);

int numberOfEmployeeNumbers = Empnum.length;

String employeeNumber = Empnum[ran.nextInt(numberOfEmployeeNumbers)];
tempSworker.setEmployeeNumber(employeeNumber);

int yearHired = ran.nextInt(35) + 1900;
tempSworker.setEmployeehireyear(yearHired);

double weeklySalary = ran.nextInt((100) * 100);
tempSworker.setEmployeeweeklyearning(weeklySalary);

int goalsMet = ran.nextInt(100) + 1;
tempSworker.setGoalsCleared(goalsMet);

double yearlySalary = ran.nextInt((40000) + 40000);
tempSworker.setYearlySalary(yearlySalary);

sworker = tempSworker;

return tempSworker;

}

public int checkSize() {
return empSheet.size();
}
}

继承人类(class):

import java.util.*;

public class Shift
extends EmployeeSheet {

private ArrayList<Employee> shiftDetails;
private ProductionWorker pWorker;
private ShiftSupervisor sWorker;

public Shift() {
this.generateShift();

}
// instance variables - replace the example below with your own


/**
* Constructor for objects of class Shift
*/

public void generateShift() {
shiftDetails = new ArrayList();

Random supervisorCount = new Random();
int ssID = supervisorCount.nextInt(3);
for (int i = 0; i < ssID; i++) {

shiftDetails.add(this.generateShiftSupervisor());
}

Random productionCount = new Random();
int prodID = productionCount.nextInt(15) + 5;
for (int d = 0; d < prodID; d++) {

shiftDetails.add(this.generateProductionWorker());
}
}

public ProductionWorker generateProductionWorker() {
pWorker = super.generateProductionWorker();
return pWorker;
}
public ShiftSupervisor generateShiftSupervisor() {
sWorker = super.generateShiftSupervisor();
return sWorker;

}

}

知道为什么会这样吗?

最佳答案

Why does subclass run Superclass constructor?

构造基础对象。否则它将是未构造的并且继承语义将是不完整的。

everytime I create the shift object it displays the EmployeeSheet fields.

因为 EmployeeSheet 的构造函数显示了这些字段。

Any ideas why this is happening?

因为 Shift 扩展了 EmployeeSheet。

关于java - 为什么子类运行父类(super class)构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28921554/

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