我正在读一本有关Java的书,目前正在阅读有关Polymorphism
主题以及如何downcast
一个参考变量的书。但是,我非常了解向下转换的概念。下面是我要遵循的示例的uml。
对于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
的方法Employee
和currentEmployee
,并在每次迭代时将其引用分配给数组中不同的Employee
。结果,输出说明了基于对象的类型,每个类的特定方法都被调用并在执行时解析。为了在当前
BasePlusCommissionEmployee
对象上调用getBaseSalary
的方法setBaseSalary
和Employee
,条件语句用于通过使用运算符的实例来检查对象引用是否为BasePlusCommissionEmployee
对象,如果条件为true,则对象必须为在调用上述方法之前将从Employee
转换为BasePlusCommissionEmployee
类型。这使我非常困惑,因为我们能够访问子类的
toString
方法,但是必须向下转换对象才能使用其他方法,即getBaseSalary
和setBaseSalary
?为什么会这样呢? 最佳答案
因为toString()
是在Object
中定义的,所以在每个类中都可用。基本薪资的getter和setter仅在BasePlusCommissionEmployee
中可用,因此您不能通过Employee
引用来调用它(如果引用不同的类型会发生什么?)。
这个示例不是您在实际代码中看到的。使用instanceof确定要做什么是不好的风格。
关于java - 多态和垂头丧气的问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41146739/