java中的抽象类getter和setter

时间:2013-04-16 22:31:17

标签: java arraylist abstract-class

所以,解释我的代码 - >

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

然后我从Employee扩展了Admin,Sales和Manager类。这些都有自己的字段,例如admin的fixedBonus,销售的percentBonus和经理的雇员的arraylist。

然后我有一个包含Employees的ArrayList的驱动程序类,因此它包含Admins,Sales和Managers。

我的问题出现在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;
}

3 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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的重点在于,即使你不知道你正在使用什么样的物体,你仍然可以使用它们。

答案 2 :(得分:0)

使您的客户端代码 lot 更简单(并且更干净 - instanceof和cast难看)的选项是为Employee中的每个特定方法声明一个默认实现,例如return fixedBonus()中的Employee为零,但会在Sales

中覆盖它