BasePlusCommissionEmployee

BasePlusCommissionEmployee

我正在读一本有关Java的书,目前正在阅读有关Polymorphism主题以及如何downcast一个参考变量的书。但是,我非常了解向下转换的概念。下面是我要遵循的示例的uml。

java - 多态和垂头丧气的问题-LMLPHP

对于BasePlusCommissionEmployee的所有对象,他们的基本工资将增加10%。其他Employee子类是正常的。 PayrollSystemTest包含运行应用程序的主要方法。

// Fig. 10.9: PayrollSystemTest.java
// Employee hierarchy test program.

public class PayrollSystemTest
{
    public static void main(String[] args)
    {
        // create subclass objects
        SalariedEmployee salariedEmployee =
            new SalariedEmployee("John", "Smith", "111-11-1111", 800.00);
        HourlyEmployee hourlyEmployee =
            new HourlyEmployee("Karen", "Price", "222-22-2222", 16.75, 40.0);
        CommissionEmployee commissionEmployee =
            new CommissionEmployee(
            "Sue", "Jones", "333-33-3333", 10000, .06);
        BasePlusCommissionEmployee basePlusCommissionEmployee =
            new BasePlusCommissionEmployee(
            "Bob", "Lewis", "444-44-4444", 5000, .04, 300);

        System.out.println("Employee processed individually:");

        System.out.printf("%n%s%n%s: $%,.2f%n%n",
            salariedEmployee, "earned", salariedEmployee.earnings());
        System.out.printf("%s%n%s: $%,.2f%n%n",
            hourlyEmployee, "earned", hourlyEmployee.earnings());
        System.out.printf("%s%n%s: $%,.2f%n%n",
            commissionEmployee, "earned", commissionEmployee.earnings());
        System.out.printf("%s%n%s: $%,.2f%n%n",
            basePlusCommissionEmployee,
            "earned", basePlusCommissionEmployee.earnings());

        // create four-element Employee array
        Employee[] employees = new Employee[4];

        // initialize array with Employees
        employees[0] = salariedEmployee;
        employees[1] = hourlyEmployee;
        employees[2] = commissionEmployee;
        employees[3] = basePlusCommissionEmployee;

        System.out.printf("Employees processed polymorphically:%n%n");

        // generically process each element in array employees
        for (Employee currentEmployee : employees)
        {
            System.out.println(currentEmployee); // invokes toString

            // determine whether element is a BasePlusCommissionEmployee
            if (currentEmployee instanceof BasePlusCommissionEmployee)
            {
                // downcast Employee reference to
                // BasePlusCommissionEmployee reference
                BasePlusCommissionEmployee employee =
                    (BasePlusCommissionEmployee) currentEmployee;

                employee.setBaseSalary(1.10 * employee.getBaseSalary());

                System.out.printf(
                    "new base salary with 10%% increase is: $%,.2f%n",
                    employee.getBaseSalary());
            } // end if

            System.out.printf(
                "earned $%,.2f%n%n", currentEmployee.earnings());
        }  // end for

        // get type name of each object in employees array
        for (int j = 0; j < employees.length; j++)
            System.out.printf("Employee %d is a %s%n", j,
                employees[j].getClass().getName());
    } // end main
} // end class PayrollSystemTest

该书进一步解释说,增强的for循环对数组employees进行迭代,并调用带有toString变量earnings的方法EmployeecurrentEmployee,并在每次迭代时将其引用分配给数组中不同的Employee。结果,输出说明了基于对象的类型,每个类的特定方法都被调用并在执行时解析。

为了在当前BasePlusCommissionEmployee对象上调用getBaseSalary的方法setBaseSalaryEmployee,条件语句用于通过使用运算符的实例来检查对象引用是否为BasePlusCommissionEmployee对象,如果条件为true,则对象必须为在调用上述方法之前将Employee转换为BasePlusCommissionEmployee类型。

这使我非常困惑,因为我们能够访问子类的toString方法,但是必须向下转换对象才能使用其他方法,即getBaseSalarysetBaseSalary?为什么会这样呢?

最佳答案

因为toString()是在Object中定义的,所以在每个类中都可用。基本薪资的getter和setter仅在BasePlusCommissionEmployee中可用,因此您不能通过Employee引用来调用它(如果引用不同的类型会发生什么?)。

这个示例不是您在实际代码中看到的。使用instanceof确定要做什么是不好的风格。

关于java - 多态和垂头丧气的问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41146739/

10-09 02:59