簡體   English   中英

構造函數不創建對象-Java

[英]Constructors not Creating Object — Java

大約一個星期以來,我一直在自己的一個程序中解決此問題,但我不知道自己在做什么錯。 對於此分配,我基本上是用3種不同類型的雇員(佣金,薪水,小時數)組成一個Employee對象數組。 這三個子類都擴展了Employee類,它是抽象的。

這是我的代碼,它為每種類類型調用構造函數:

    public boolean addEmployee(int empType, String first, String last, char mi, char gen, int empNum, boolean full, double amount)
    {
    employeeCount += 1;

    if (employeeCount > EMPLOYEES_MAX) {
        System.out.println("ERROR: ARRAY FULL. UNABLE TO ADD NEW EMPLOYEE");
        return false;
    }

    else {
        switch (empType) {
            case 1:
            employees[employeeCount] = new HourlyEmployee(first, last, mi, gen, empNum, full, amount);
            break;

            case 2:
            employees[employeeCount] = new SalaryEmployee(first, last, mi, gen, empNum, full, amount);
            break;

            case 3:
            employees[employeeCount] = new CommissionEmployee(first, last, mi, gen, empNum, full, amount);
            break;
        }
        return true;
    }
}

這是薪水員工的構造函數,其他兩個與此基本相同。

public class SalaryEmployee extends Employee
{
private double salary;

public SalaryEmployee(String first, String last, char midInit, char gen, int empNum, boolean full, double sal)
{
    super(first, last, midInit, gen, empNum, full);
    salary = sal;
}

我編寫的由該構造函數調用的超級構造函數如下:

abstract class Employee
{
protected String firstName;
protected String lastName;
protected char middleInitial;
protected boolean fullTime;
protected char gender;
protected int employeeNum;

public Employee(String first, String last, char midInit, char gen, int empNum, boolean full)
{
    firstName = first;
    lastName = last;
    middleInitial = midInit;
    employeeNum = empNum;
    fullTime = full;

    switch (gen) {
        case 'M': case 'F': 
        case 'm': case 'f':
        gender = gen;
        break;

        default:
        gender = 'F';
    }
}

就像我之前說過的,我不知道為什么它不創建應有的對象。 每當我打印輸出數組或對其進行排序時,總是得到null或null指針異常。

任何幫助將不勝感激,非常感謝大家!

編輯:

好的,這里是我的Employee數組初始化的地方。

public class EmployeeList
{
private final int EMPLOYEES_MAX = 50;
private Employee employees[];
private int employeeCount;

public EmployeeList()
{
    employees = new Employee[EMPLOYEES_MAX];
    employeeCount = 0;
}

當我運行它時,由於未運行默認方法,就像仍在構造函數中運行一樣,由於某種奇怪的原因,我只會得到null。 繼承人的輸出:

從以下選項中選擇:1添加員工2按類型處理員工子菜單3刪除員工4排序員工5計算每周支付6計算獎金7年度薪水8重設周0退出選項:1

  1. 每小時
  2. 薪水
  3. 佣金輸入選項:1

輸入姓氏:Doe

輸入名字:Jane

輸入中間名首字母:M

輸入性別:F

輸入員工編號:1

全職? (y / n):y

輸入工資:14

員工已添加到列表

空值

主菜單

null來自我的主類,該主類調用listAll()方法,該方法應列出數組中的所有內容。 但只會返回null。

再次編輯:

因此,這里是應該列出所有內容的方法。

    public void listAll()
{
    for(int x = 0; x < employeeCount; x++)
        System.out.println(employees[x]);
}

謝謝你的幫助!

第一個問題

您傳入int empType ,然后打開employeeType

第二個問題

您傳遞了int empType ,但您的開關中的所有情況都是char ,例如case '1': int empType

編輯:您已編輯問題,以解決某些問題>:-/

第三個問題

在您的listAll()方法中,您試圖打印不具有toString()方法的對象。 給您的抽象Employee類一個toString()方法,如下所示。

public String toString() {
    return lastName + ", " + firstName + " " + middleInitial;
}

現在,假設您希望子類具有更詳細的表示形式,例如“ Smith,John C.每小時。 ”您可以通過向每個子類中添加ANOTHER toString方法來做到這一點,但是您必須重用已經存在的部分寫了! 例如,這將在您的每小時員工班級中進行。

public String toString() {
    return super.toString() + " is hourly.";
}

我想到了幾件事情,但其中大多數都考慮了總體設計,而不是您的特定問題。

一對一

您的代碼當前包含一個錯誤的外觀。 您正在將employeeCount初始化為0但是在第一次插入之前將其遞增為1 因此, 第一名員工進入了employees[1]第二個陣列插槽。

但是您的輸出循環是正確的,並且在第一次插入之后,它僅輸出employees[0] ,該值仍然為null 只需移動employeeCount += 1; 到方法的結尾,您就可以了。

(在不清楚的情況下:Java計算從0length-1數組索引。)

IndexOutOfBounds

還有另一個錯誤。 您正在比較employeeCount > EMPLOYEES_MAX 如果employeeCount = EMPLOYEES_MAX ,則它通過了測試,但是在employees[EMPLOYEES_MAX]處插入將失敗,因為它超出了數組的范圍。

您必須檢查employeeCount >= EMPLOYEES_MAX 這似乎很違反直覺,因為在您的方法中,您將其用作“此插入之后的員工數” 如果從上方包括我的更正,則解釋為“已插入的員工數”

case '1'更改為case 1 ,依此類推,當您傳入int時,顯然不想打開char值。

到目前為止,我已經加入了您所寫的內容,並且看起來行之有效(為提高代碼可讀性而引入了常量):

public class EmployeeList {

public static final int EMP_TYPE_HOURLY = 1;
public static final int EMP_TYPE_SALARY = 2;
public static final int EMP_TYPE_COMMISSION = 3;
public static final char EMP_GEN_MALE = 'M';
public static final char EMP_GEN_FEMALE = 'F';

private int employeeCount = 0;
private static final int EMPLOYEES_MAX = 5;
private Employee[] employees = new Employee[EMPLOYEES_MAX];

public boolean addEmployee(int empType, String first, String last, char mi, char gen, int empNum, boolean full, double amount) {
    employeeCount += 1;

    if (employeeCount > EMPLOYEES_MAX) {
        System.out.println("ERROR: ARRAY FULL. UNABLE TO ADD NEW EMPLOYEE");
        return false;
    } else {
        switch (empType) {
            case EMP_TYPE_HOURLY:
                employees[employeeCount] = new HourlyEmployee(first, last, mi, gen, empNum, full, amount);
                break;

            case EMP_TYPE_SALARY:
                employees[employeeCount] = new SalaryEmployee(first, last, mi, gen, empNum, full, amount);
                break;

            case EMP_TYPE_COMMISSION:
                employees[employeeCount] = new CommissionEmployee(first, last, mi, gen, empNum, full, amount);
                break;
        }
        return true;
    }
}

public Employee[] getEmployees() {
    return employees;
}

public static void main(String[] args) {
    EmployeeList empList = new EmployeeList();

    empList.addEmployee(EMP_TYPE_HOURLY, null, null, 'A', EMP_GEN_MALE, 555, true, 100.0);
    empList.addEmployee(EMP_TYPE_HOURLY, null, null, 'A', EMP_GEN_FEMALE, 555, true, 100.0);
    empList.addEmployee(EMP_TYPE_SALARY, null, null, 'A', EMP_GEN_FEMALE, 555, true, 100.0);
    empList.addEmployee(EMP_TYPE_COMMISSION, null, null, 'A', EMP_GEN_FEMALE, 555, true, 100.0);

    for (int i = 0; i < EMPLOYEES_MAX; i++) {
        System.out.println("" + empList.getEmployees()[i]);
    }
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM