題目 1 · Short Answer
7.4 分An object-oriented program contains a base class called \(\text{Vehicle}\) and two subclasses, \(\text{ElectricCar}\) and \(\text{PetrolCar}\). The \(\text{Vehicle}\) class has a virtual method \(\text{calculateRange()}\). Both subclasses override this method.
Describe what is meant by **polymorphism** and **dynamic binding** in this context, and explain how they allow a list of \(\text{Vehicle}\) objects to be processed efficiently.
Describe what is meant by **polymorphism** and **dynamic binding** in this context, and explain how they allow a list of \(\text{Vehicle}\) objects to be processed efficiently.
查看答案詳解收起答案詳解
解題
### 1. Polymorphism in this context
- Polymorphism is the ability of different objects to respond to the same method call in different ways.
- In this scenario, the program can store objects of types \(\text{ElectricCar}\) and \(\text{PetrolCar}\) in a single collection of type \(\text{Vehicle}\). When the method \(\text{calculateRange()}\) is called on a reference of type \(\text{Vehicle}\), the correct subclass implementation (either electric or petrol) is executed.
### 2. Dynamic Binding (Late Binding)
- Dynamic binding is when the association between a method call and its actual implementation is made at runtime rather than at compile-time.
- The compiler does not hardcode the memory address of the function to execute. Instead, it is resolved during program execution based on the actual runtime object type of the vehicle.
### 3. Efficient Processing of the List
- Without polymorphism/dynamic binding, the programmer would have to write complex conditional structures (e.g., `if` or `switch` statements) to check the type of each vehicle in the list and call the corresponding specific method.
- With polymorphism, the program can simply iterate through a list of `Vehicle` objects using a simple loop and execute `vehicle.calculateRange()`. This reduces code complexity, eliminates execution overhead from multiple conditional checks, and makes the system easily extensible (e.g., adding a `HybridCar` subclass requires no changes to the processing loop).
- Polymorphism is the ability of different objects to respond to the same method call in different ways.
- In this scenario, the program can store objects of types \(\text{ElectricCar}\) and \(\text{PetrolCar}\) in a single collection of type \(\text{Vehicle}\). When the method \(\text{calculateRange()}\) is called on a reference of type \(\text{Vehicle}\), the correct subclass implementation (either electric or petrol) is executed.
### 2. Dynamic Binding (Late Binding)
- Dynamic binding is when the association between a method call and its actual implementation is made at runtime rather than at compile-time.
- The compiler does not hardcode the memory address of the function to execute. Instead, it is resolved during program execution based on the actual runtime object type of the vehicle.
### 3. Efficient Processing of the List
- Without polymorphism/dynamic binding, the programmer would have to write complex conditional structures (e.g., `if` or `switch` statements) to check the type of each vehicle in the list and call the corresponding specific method.
- With polymorphism, the program can simply iterate through a list of `Vehicle` objects using a simple loop and execute `vehicle.calculateRange()`. This reduces code complexity, eliminates execution overhead from multiple conditional checks, and makes the system easily extensible (e.g., adding a `HybridCar` subclass requires no changes to the processing loop).
評分準則
Award marks as follows (up to 7.4 marks total):
- **Up to 2 marks for Polymorphism**:
- 1 mark: Define polymorphism as the ability of different objects to respond to the same method call differently.
- 1 mark: Apply it to the scenario (e.g., calling `calculateRange()` on a `Vehicle` variable runs the specific overridden code in `ElectricCar` or `PetrolCar`).
- **Up to 2 marks for Dynamic Binding**:
- 1 mark: Define dynamic binding as resolving the association of a method call to its implementation at runtime (rather than compile-time).
- 1 mark: Explain that it uses the actual object type at execution time to find the correct memory location of the overridden method.
- **Up to 3.4 marks for Efficiency of processing**:
- 1 mark: Explain that it allows a single uniform loop to process a mixed list of `Vehicle` objects.
- 1 mark: Explain that it eliminates the need for conditional branches (e.g., `if-else` type-checking) to find the object's class.
- 1.4 marks: Explain that it facilitates high extensibility (new classes can be added without altering the loop code).
- **Up to 2 marks for Polymorphism**:
- 1 mark: Define polymorphism as the ability of different objects to respond to the same method call differently.
- 1 mark: Apply it to the scenario (e.g., calling `calculateRange()` on a `Vehicle` variable runs the specific overridden code in `ElectricCar` or `PetrolCar`).
- **Up to 2 marks for Dynamic Binding**:
- 1 mark: Define dynamic binding as resolving the association of a method call to its implementation at runtime (rather than compile-time).
- 1 mark: Explain that it uses the actual object type at execution time to find the correct memory location of the overridden method.
- **Up to 3.4 marks for Efficiency of processing**:
- 1 mark: Explain that it allows a single uniform loop to process a mixed list of `Vehicle` objects.
- 1 mark: Explain that it eliminates the need for conditional branches (e.g., `if-else` type-checking) to find the object's class.
- 1.4 marks: Explain that it facilitates high extensibility (new classes can be added without altering the loop code).