📚 Object-Oriented Programming for Edexcel A-Level | Edexcel A-Level 编程:面向对象编程核心考点
Object-oriented programming (OOP) is one of the most heavily assessed programming paradigms in Edexcel A-Level Computer Science. It moves beyond simple sequence, selection and iteration by organising code into classes and objects that model real-world entities. This revision article covers the exact OOP concepts required by the Pearson Edexcel specification, including classes, objects, encapsulation, inheritance, polymorphism and the relationships between objects. Each section contains exam-focused explanations, code examples and common pitfalls.
面向对象编程(OOP)是 Edexcel A-Level 计算机科学中考查最密集的编程范式之一。它通过将代码组织为模拟现实世界实体的类和对象,超越了简单的顺序、选择和迭代结构。本篇复习文章涵盖 Pearson Edexcel 大纲所要求的 OOP 核心概念,包括类、对象、封装、继承、多态以及对象之间的关系。每节都包含面向考试的解释、代码示例和常见易错点。
1. Why OOP Matters in the Edexcel Specification | 为什么 OOP 在 Edexcel 大纲中重要
Edexcel A-Level Computer Science requires you to compare programming paradigms and justify the use of OOP for large, maintainable systems. OOP allows the same class to be reused in different programs, hides internal data to reduce accidental errors, and models inheritance from general to specialised types.
Edexcel A-Level 计算机科学要求你比较编程范式,并说明如何在大规模、可维护系统中合理使用 OOP。OOP 允许同一个类在不同程序中复用,隐藏内部数据以减少意外错误,并通过继承从一般类型建模到专用类型。
- OOP is not simply ‘using classes’ — it requires encapsulation, inheritance and polymorphism. | OOP 不只是使用类,它需要封装、继承和多态。
- Edexcel exam questions often ask you to identify classes from a scenario or trace OOP code. | Edexcel 试题常要求你根据情景识别类或跟踪 OOP 代码。
2. Classes and Objects | 类与对象
A class is a blueprint or template that defines the attributes and methods shared by its objects. An object is a specific instance created from a class. For example, the class Animal may have attributes name and age, and a method speak. Creating an object Dog from Animal gives those attributes specific values.
类是定义其对象所共享的属性与方法的蓝图或模板。对象是从类创建的具体实例。例如,类 Animal 可以具有属性 name 和 age,以及方法 speak。从 Animal 创建对象 Dog 会为这些属性赋予具体值。
| Term | 术语 | Definition | 定义 |
|---|---|
| Class | A blueprint for creating objects. | 用于创建对象的蓝图。 |
| Object | An instance of a class with its own state. | 类的实例,拥有自己的状态。 |
| Attribute | Data stored inside an object. | 存储在对象内部的数据。 |
| Method | A function defined inside a class. | 在类内部定义的函数。 |
| Constructor | Special method used to initialise new objects. | 用于初始化新对象的特殊方法。 |
The code below shows a simple class, its constructor and object instantiation in Python-style pseudocode.
下面的代码以 Python 风格伪代码展示了一个简单类、其构造函数和对象实例化。
class Animal:
def __init__(self, name, age):
self.name = name
self.age = age
def speak(self):
return "Some sound"
class Dog(Animal):
def speak(self):
return "Woof"
d = Dog("Rex", 3)
print(d.name, d.age, d.speak())
3. Encapsulation | 封装
Encapsulation means bundling data and the methods that operate on that data inside a single class, while restricting direct access to some fields. In Edexcel terms, a class offers a public interface, but the internal representation is hidden. This supports maintainability because you can change internal implementation without breaking external code.
封装是指将数据和操作这些数据的方法捆绑在一个类中,同时限制对某些字段的直接访问。在 Edexcel 术语中,类提供一个公共接口,但内部表示被隐藏。这提高了可维护性,因为你可以在不破坏外部代码的情况下更改内部实现。
- Public: accessible from anywhere. | 公共:任何地方都可访问。
- Private: accessible only inside the class. | 私有:只能在类内部访问。
- Protected: accessible in the class and derived classes. | 保护:在类及其派生类中可访问。
A typical Edexcel question might ask you to explain how encapsulation prevents invalid data. By making attributes private and providing getter and setter methods, you can validate data before it is stored. This reduces the risk of negative ages, empty names or invalid marks.
典型的 Edexcel 问题可能要求你解释封装如何防止无效数据。通过将属性设为私有并提供 getter 和 setter 方法,你可以在存储前验证数据。这可以降低负年龄、空名称或无效成绩等风险。
4. Inheritance | 继承
Inheritance allows a new class (subclass) to acquire the properties and methods of an existing class (superclass). This models an ‘is-a’ relationship and avoids code duplication. In the example above, Dog inherits name and age from Animal and overrides speak. Edexcel expects you to identify when inheritance is appropriate and distinguish it from association.
继承允许新类(子类)获得现有类(父类)的属性和方法。它建模“是一种”关系,避免代码重复。在上面的例子中,Dog 从 Animal 继承了 name 和 age 并重写了 speak。Edexcel 要求你判断何时适合使用继承,并将其与关联关系区分开。
The key phrase is ‘Dog is an Animal’. If the relationship sounds like ‘has a’, such as ‘Car has an Engine’, then you should use aggregation or composition instead of inheritance. Inheritance should only be used when a clear hierarchical ‘is-a’ relationship exists.
关键短语是“Dog 是 Animal”。如果关系听起来像“有一个”,例如“Car 有 Engine”,则应使用聚合或组合而不是继承。只有当存在明确的层级“是一种”关系时,才应使用继承。
5. Polymorphism | 多态
Polymorphism means ‘many forms’. In OOP it allows a single variable declared as the base class to refer to objects of different subclasses. When the same method name is called, the actual subclass version executes. This is method overriding and is a runtime decision. Polymorphism enables flexible and scalable code, because a list of Animal objects can contain Dog, Cat or Bird objects and each responds correctly to speak.
多态意为“多种形态”。在 OOP 中,它允许声明为基类的单个变量引用不同子类的对象。当调用相同的方法名时,实际执行的是子类版本。这就是方法重写,属于运行时决定。多态使代码灵活且可扩展,因为一个 Animal 对象列表可以包含 Dog、Cat 或 Bird 对象,每个对象都能正确响应 speak。
animals = [Dog("Rex", 3), Cat("Puss", 2), Bird("Rio", 1)]
for a in animals:
print(a.speak())
In the exam, you may be given similar code and asked to state the output. You must check each object’s actual class when speak is called, not the list type. This shows dynamic dispatch, a key feature of polymorphism.
在考试中,你可能会看到类似代码并要求说明输出。你必须检查每个对象的实际类,而不是列表类型。这展示了动态分派,这是多态的关键特征。
6. Association, Aggregation and Composition | 关联、聚合与组合
These relations describe how objects use each other. Association means objects know about each other but both can exist independently. Aggregation is a ‘has-a’ relationship where the whole contains parts, but parts can exist without the whole. Composition is a stronger ‘has-a’ relationship where parts cannot exist without the whole. Edexcel often asks for examples from a scenario.
这些关系描述对象之间如何
Published by TutorHao | A-Level 编程 Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导Cancel reply