0

所以,解释我的代码->

我有一个Employee类型的抽象类,它有自己的字段,比如名字。

然后,我拥有从 Employee 扩展而来的 Admin、Sales 和 Manager 类。这些都有自己的字段,例如管理员的固定奖金、销售的百分比奖金和经理的员工数组列表。

然后我有一个包含员工 ArrayList 的驱动程序类,因此它包含管理员、销售和经理。

我的问题出现在 Arraylist 元素上。假设我从 ArrayList 中获得了一个 Admin 类的员工,并尝试使用 getter getFixedBonus() 访问他们的 fixedBonus 字段,但我根本做不到。我怎样才能做到这一点,以便我可以使用特定于类的 getter 和 setter,而不仅仅是继承的 getter 和 setter?这是我的代码的副本,其中出现了问题。具体来说,问题出在 switch 语句的第 5 和第 6 种情况下。

private Employee editAnEmployee(Employee emp) {
    boolean exit = false;
    boolean validChoice = false;
    int choice = 0;
    Boolean sure = false; // used in the switch statement
    String newName;
    double newValue;
    Employee admin = new AdminWorker("0", "0", 0, 0, 0); // these are used
    Employee sales = new SalesWorker("0", "0", 0, 0, 0, 0); // to check the
    Employee manager = new Manager("0", "0", 0, 0); // class of the
                                                    // employee
                                                    // being sent
                                                    // in, to edit
                                                    // the fields
                                                    // the employee
                                                    // may hold
    do {
        do {
            validChoice = true;
            emp.toString();
            StdOut.println("Which field would you like to edit?");
            StdOut.println("1) First Name.");
            StdOut.println("2) last Name.");
            StdOut.println("3) Hourly rate.");
            StdOut.println("4) Hours worked in the last week.");
            if (emp.getClass().equals(admin)) {
                StdOut.println("5) Fixed Bonus.");
            } else if (emp.getClass().equals(sales)) {
                StdOut.println("5) Percentage bonus on sales.");
                StdOut.println("6) Value of sales made in the last week.");
            } else if (emp.getClass().equals(manager)) {
                StdOut.println("5) Department.");
            }
            StdOut.println("0) Exit.");
            choice = StdIn.readInt();
            if (choice < 0 || choice > 4) {
                if (emp.getClass().equals(admin) && choice <= 5) {
                    validChoice = true;
                } else if (emp.getClass().equals(sales) && choice <= 6) {
                    validChoice = true;
                } else if (emp.getClass().equals(manager)) {
                    validChoice = true;
                } else {
                    StdOut.println("You entered an invalid number! Try Again...");
                    validChoice = false;
                }
            }
        } while (!validChoice);
        switch (choice) {
        case 0:
            StdOut.println("You are now exiting...");
            exit = true;
            break;
        case 1:
            StdOut.println("The employees first name is: " + emp.getFirstName() + ". What would you like the first name to be now?");
            newName = StdIn.readString();
            StdOut.println("Are you sure you want to change " + emp.getFirstName() + " to " + newName +"?(y/n)");
            sure = ynChoice();
            if(sure) emp.setFirstName(newName);
            break;
        case 2:
            StdOut.println("The employees last name is: " + emp.getLastName() + ". What would you like the last name to be now?");
            newName = StdIn.readString();
            StdOut.println("Are you sure you want to change " + emp.getLastName() + " to " + newName +"?(y/n)");
            sure = ynChoice();
            if(sure) emp.setLastName(newName);
            break;
        case 3:
            StdOut.println("The employees hourly rate is: " + emp.getHourlyRate() + ". What would you like the hourly rate to be now?");
            newValue = StdIn.readDouble();
            StdOut.println("Are you sure you want to change " + emp.getHourlyRate() + " to " + newValue +"?(y/n)");
            sure = ynChoice();
            if(sure) emp.setHourlyRate(newValue);
            break;
        case 4:
            StdOut.println("The employee has worked: " + emp.getHoursWorked() + " hours in the last week. What would you like that to be now?");
            newValue = StdIn.readDouble();
            StdOut.println("Are you sure you want to change " + emp.getHoursWorked() + " to " + newValue +"?(y/n)");
            sure = ynChoice();
            if(sure) emp.setHoursWorked(newValue);
            break;
        case 5:
            if (emp.getClass().equals(admin)) {
                StdOut.println("The employees fixed bonus is: " + emp.getFixedBonus() + ". What would you like that to be now?");
                newValue = StdIn.readDouble();
                StdOut.println("Are you sure you want to change " + emp.getFixedBonus() + " to " + newValue +"?(y/n)");
                sure = ynChoice();
                if(sure) emp.setHoursWorked(newValue);
            } else if (emp.getClass().equals(sales)) {
                StdOut.println("The employees percentage bonus is: " + emp.getPercentageBonus() + ". What would you like that to be now?");
                newValue = StdIn.readDouble();
                StdOut.println("Are you sure you want to change " + emp.getPercentageBonus() + " to " + newValue +"?(y/n)");
                sure = ynChoice();
                if(sure) emp.setHoursWorked(newValue);
            } else if (emp.getClass().equals(manager)) {
                StdOut.println("The employees fixed bonus is: " + emp.getFixedBonus() + ". What would you like that to be now?");
                newValue = StdIn.readDouble();
                StdOut.println("Are you sure you want to change " + emp.getFixedBonus() + " to " + newValue +"?(y/n)");
                sure = ynChoice();
                if(sure) emp.setHoursWorked(newValue);
            }
            break;
        case 6:
            StdOut.println("The employees sales in the last week are: " + emp.getLastWeeksSales() + ". What would you like that to be now?");
            newValue = StdIn.readDouble();
            StdOut.println("Are you sure you want to change " + emp.getLastWeeksSales() + " to " + newValue +"?(y/n)");
            sure = ynChoice();
            if(sure) emp.setHoursWorked(newValue);
            break;
        }
    } while (!exit);
    return emp;
}
4

3 回答 3

3

我认为instanceof关键字在您的情况下可能很有用。

ArrayList<Employee> list; // This is initialized and has Employees

for (Employee person: list) {
    if (person instanceof Admin) {
        int fixedBonus = ((Admin) person).getFixedBonus();

        // Do something here
    }
}

这是否回答你的问题?(您不需要 .getClass().equals(),这就是 instanceof 的用途:D)

于 2013-04-16T22:43:12.983 回答
2

您应该为您的员工添加getFixedBonus()getPercentBonus()方法,每个返回 0。然后其他工人类可以覆盖它们。

实际上,一个更好的技术是把它变成一个interface并让所有的类(包括Employee)实现interface. 然后,他们可以extend创建一个base也实现接口但提供默认值的类。

public static interface Staff {
  public double getFixedBonus ();
  public double getPercentageBonus ();
  public List<Staff> getMinions();
}

// Base class - no bonus by default.
public static class Worker implements Staff {

  @Override
  public double getFixedBonus() {
    return 0;
  }

  @Override
  public double getPercentageBonus() {
    return 0;
  }

  @Override
  public List<Staff> getMinions() {
    return Collections.EMPTY_LIST;
  }

}

public static class Administrator extends Worker implements Staff {
  double fixedBonus = 2.13;

  @Override
  public double getFixedBonus() {
    return fixedBonus;
  }

}

public static class Salesperson extends Worker implements Staff {
  double percentageBonus = 3.14;

  @Override
  public double getPercentageBonus() {
    return percentageBonus;
  }

}

public static class Manager extends Worker implements Staff {
  List<Staff> minions;

  @Override
  public List<Staff> getMinions() {
    return minions;
  }
}

不要被使用instanceof和铸造的阴暗面所诱惑。你会后悔你的决定。oop 的全部意义在于,即使您不知道自己正在处理什么样的对象,但您仍然可以处理它们。

于 2013-04-16T22:56:00.490 回答
0

使您的客户端代码更简单(并且更简洁 - instanceof 和强制转换很难看)的选项为 in 中的每个特定方法声明一个默认实现Employee,例如为fixedBonus()in返回零Employee,但在Sales

于 2013-04-16T23:20:21.950 回答