Given the following classes:
public class Employee {
public int salary;
}
public class Manager extends Employee {
public int budget;
}
public class Director extends Manager {
public int stockOptions;
}
And given the follow main method:
public static void main(String[] args){
Employee employee = new Employee();
Manager manager = new Manager();
Director director = new Director();
// line n1
}
Which two options fail to compile when placed at line n1 of the main method?
A.
employee.salary = 50_000;
B.
director.salary = 80_000;
C.
employee.budget = 200_000;
D.
manager.budget = 1_000_000;
E.
manager.stockOption = 500;
F.
director.stockOptions = 1_000;
题解
选项 A,Employee 对象本身有 salary 字段,因此不会有问题。
选项 B,Director 对象也是 Employee,有 salary 字段,因此也不会有问题。
选项 C,Employee 对象并没有 budget 字段,编译会有问题。
选项 D,Manager 对象本身有 budget 字段,因此不会有问题。
选项 E,Manager 对象并没有 stockOption 字段,编译会有问题。
选项 F,Director 对象本身有 stockOptions 字段,因此不会有问题。
