📚 Object-Oriented Programming (OOP) for Edexcel A-Level Computer Science | 面向对象编程 (OOP) 考点精讲
Object-oriented programming (OOP) is a central programming paradigm in the Edexcel A-Level Computer Science specification. It organises code into classes and objects, making programs easier to design, maintain and reuse. This article explains the key OOP concepts you need to master, from classes and objects to inheritance and polymorphism, with exam-focused examples.
面向对象编程 (OOP) 是 Edexcel A-Level 计算机科学大纲中的核心编程范式。它把代码组织为类和对象,使程序更易于设计、维护和复用。本文讲解你需要掌握的关键 OOP 概念,从类和对象到继承和多态,并提供贴近考试的示例。
1. Programming Paradigms: Where OOP Fits | 编程范式:OOP 的定位
Before studying OOP, it is useful to compare it with other paradigms. Procedural programming uses sequences of instructions and functions; functional programming treats computation as evaluation of mathematical functions; OOP models a system as interacting objects. Edexcel questions often ask you to justify choosing an OOP approach.
在学习 OOP 之前,先与其他范式比较会很有帮助。过程式编程使用指令序列和函数;函数式编程把计算视为数学函数的求值;OOP 则将系统建模为相互交互的对象。Edexcel 考题常要求你说明选择 OOP 方法的理由。
| Paradigm | Key idea | Typical languages |
|---|---|---|
| Procedural | Step-by-step instructions and procedures | C, Pascal |
| Functional | Evaluation of pure functions, no side effects | Haskell, Lisp |
| Object-oriented | Classes, objects, encapsulation, inheritance | Java, Python, C++ |
The table summarises the three main paradigms. In an exam, if a scenario describes multiple entities such as students, teachers and courses, OOP is usually the natural choice because it binds data and behaviour together in objects.
表格总结了三种主要范式。在考试中,如果题目描述一个包含多种实体(如学生、教师、课程)的信息系统,OOP 通常是自然的选择,因为它能将数据和行为绑定在对象中。
2. Classes and Objects | 类与对象
A class is a blueprint or template that defines the attributes (data) and methods (behaviour) common to a group of objects. An object is a specific instance of a class. For example, a class Car might have attributes such as registration, make and colour, and methods such as accelerate() and brake(). The object myCar = Car(“AB12 CDE”, “Toyota”, “blue”) is one concrete instance.
类是一个蓝图或模板,定义了一组对象共有的属性(数据)和方法(行为)。对象是类的具体实例。例如,Car 类可以有注册号、品牌和颜色等属性,以及 accelerate() 和 brake() 等方法。对象 myCar = Car(“AB12 CDE”, “Toyota”, “blue”) 就是一个具体实例。
CLASS Car
ATTRIBUTES registration, make, colour
METHODS accelerate(), brake()
ENDCLASS
In Edexcel-style pseudocode, class definitions often look like the structure above. The exact syntax is less important than recognising that attributes hold data and methods define behaviour.
在 Edexcel 风格的伪代码中,类定义通常类似于上述结构。具体语法远不如识别“属性保存数据、方法定义行为”这一点重要。
3. Attributes and Methods | 属性与方法
Attributes store the state of an object, while methods define the operations that can be performed on that state. In Edexcel pseudocode, attributes are often declared inside a class and methods are procedures or functions that belong to the class. You should be able to identify which items in a scenario become attributes and which become methods.
属性存储对象的状态,方法定义可对该状态执行的操作。在 Edexcel 伪代码中,属性通常在类内部声明,方法则是属于类的过程或函数。你应能判断场景中哪些项应成为属性、哪些应成为方法。
For example, in a bank account scenario, the balance is an attribute because it is data, while deposit() and withdraw() are methods because they change or inspect the balance. A common exam task is to list suitable attributes and methods for a given class.
例如,在银行账户情境中,余额是属性,因为它是数据;而 deposit() 和 withdraw() 是方法,因为它们会改变或查看余额。常见考题是列出给定类合适的属性和方法。
4. Encapsulation | 封装
Encapsulation means hiding the internal state of an object and requiring all interaction to go through methods. This protects data from accidental corruption and allows the internal implementation to change without affecting other parts of the program. In OOP languages, access modifiers such as private and public control visibility.
封装是指隐藏对象的内部状态,并要求所有交互都通过方法进行。这样可以保护数据免受意外破坏,并允许内部实现更改而不影响程序的其他部分。在面向对象语言中,private 和 public 等访问修饰符控制可见性。
For Edexcel, you should know that attributes are usually declared private, while selected methods are public. Encapsulation is often tested by asking why direct access to attributes is harmful, or why getter and setter methods are used.
在 Edexcel 考试中,你应知道属性通常声明为 private,而选定的方法为 public。封装常以“为什么直接访问属性是有害的”或“为什么要使用 getter 和 setter 方法”的形式进行考查。
5. Inheritance | 继承
Inheritance allows a class (subclass) to reuse and extend the attributes and methods of another class (superclass). For example, a Car class can inherit from a Vehicle class. This promotes code reuse and models ‘is-a’ relationships. Edexcel exam questions often provide a class diagram and ask you to explain the relationship.
继承允许一个类(子类)复用并扩展另一个类(父类)的属性和方法。例如,Car 类可以继承自 Vehicle 类。这促进了代码复用,并建模了“是一种”关系。Edexcel 考题常给出类图,要求你解释这种关系。
In pseudocode, inheritance might be shown as:
在伪代码中,继承可以表示为:
CLASS Car INHERITS Vehicle
EXTRA ATTRIBUTE numberOfDoors
OVERRIDE METHOD displayDetails()
ENDCLASS
The subclass Car automatically has all the features of Vehicle, but can add new features or change existing ones. This is a high-yield exam topic, especially when combined with overriding.
子类 Car 自动拥有 Vehicle 的所有特征,但可以添加新特征或修改已有特征。这是一个高频率考点,特别是与重写结合时。
6. Polymorphism | 多态
Polymorphism means ‘many forms’. It allows the same method name to behave differently depending on the object that calls it. Method overriding is a common form: a subclass provides its own version of a method inherited from the superclass. In a list of Vehicle objects, calling vehicle.display() can produce different output for a Car, Bike or Lorry.
多态意为“多种形态”。它允许同一个方法名根据调用它的对象不同而表现出不同行为。方法重写是一种常见形式:子类提供其自己版本的方法以覆盖从父类继承的方法。在一个 Vehicle 对象列表中,调用 vehicle.display() 可以针对 Car、Bike 或 Lorry 产生不同输出。
This is powerful because a single line of code can work with many types of object without knowing their exact class at compile time. Edexcel questions sometimes ask you to describe how polymorphism improves code maintainability.
这非常强大,因为一行代码可以处理多种类型的对象,而无需在编译时知道其确切类。Edexcel 题目有时会要求你描述多态如何提高代码的可维护性。
7. Association, Aggregation and Composition | 关联、聚合与组合
These terms describe relationships between classes. Association is a general ‘uses-a’ relationship. Aggregation is a ‘has-a’ relationship where the contained object can exist independently, such as a Library having Books. Composition is a stronger ‘has-a’ relationship where the part cannot exist without the whole, such as a House having Rooms. Edexcel questions may ask you to distinguish these.
这些术语描述类之间的关系。关联是通用的“使用”关系。聚合是一种“拥有”关系,其中被包含的对象可以独立存在,例如图书馆拥有图书。组合是一种更强的“拥有”关系,其中部分不能脱离整体存在,例如房子拥有房间。Edexcel 题目可能要求你区分这些关系。
Useful exam wording: if the contained object is created and destroyed with the owner, it is composition; if it can outlive the owner, it is aggregation. Drawing clear class diagrams with labelled relationships earns marks even if the diagram is not perfect.
实用的考试措辞:如果被包含对象随所有者一起创建和销毁,则是组合;如果它能比所有者存活更久,则是聚合。即使类图不完美,清晰绘制并标注关系也能得分。
8. Advantages and Disadvantages of OOP | 面向对象的优缺点
Advantages include improved modularity, code reuse through inheritance, easier maintenance due to encapsulation, and the ability to model real-world entities naturally. Disadvantages include a steeper learning curve, increased memory overhead, and the risk of overly complex class hierarchies. You should be prepared to evaluate OOP in a given context.
优点包括更好的模块化、通过继承实现代码复用、因封装而更易维护,以及能够自然地建模现实世界实体。缺点包括学习曲线较陡、内存开销增加,以及类的层次结构可能过于复杂。你应准备好在具体情境中评价 OOP。
For high-mark questions, avoid simply listing advantages. Instead, link each point to the scenario: for example, encapsulation prevents invalid data in a banking system, while inheritance reduces duplication in a school records system.
对于高分题,不要只列优点。相反,要把每一点与情境联系起来:例如,封装可以防止银行系统中的无效数据,而继承可以减少学校记录系统中的重复。
9. Exam-Style Scenario: Modelling a School System | 考试情境:学校系统建模
Consider a school information system. You might define a Person class with attributes name and dateOfBirth, and method getAge(). Student inherits from Person and adds attributes studentID and tutorGroup; Teacher inherits from Person and adds staffID and subject. This demonstrates inheritance, encapsulation and polymorphism in a single scenario. An exam question could ask you to draw a class diagram or write pseudocode for one method.
考虑一个学校信息系统。你可以定义 Person 类,其属性为 name 和 dateOfBirth,方法为 getAge()。Student 继承自 Person,新增属性 studentID 和 tutorGroup;Teacher 继承自 Person,新增 staffID 和 subject。这在一个情境中演示了继承、封装和多态。考题可能要求你画出类图或为一个方法编写伪代码。
In such scenarios, always identify the superclass first, then decide what subclasses add. The ‘is-a’ test helps: a Student is a Person, so inheritance is valid. A Classroom is not a Person, so it should not inherit from Person; instead it may be associated with Person objects.
在这类情境中,始终先确定父类,再决定子类新增什么。“是一种”测试很有帮助:Student 是 Person,所以继承有效。Classroom 不是 Person,因此不应继承自 Person;相反,它可以与 Person 对象关联。
10. Common Pitfalls and Revision Tips | 常见误区与复习建议
A common mistake is confusing a class with an object: a class is the definition, an object is a specific instance. Another pitfall is treating inheritance as a ‘has-a’ relationship, when it should be ‘is-a’. Practise identifying attributes and methods from a passage, and be precise with access modifiers in exam answers. Use past paper scenarios to build speed.
一个常见错误是混淆类和对象:类是定义,对象是具体实例。另一个误区是把继承当作“拥有”关系,而它应该是“是一种”关系。练习从段落中识别属性和方法,并在考试答案中准确使用访问修饰符。利用历年真题情境提升答题速度。
When writing pseudocode, do not forget to declare the class, list its attributes, and show method signatures. Even if you cannot write perfect code, structured pseudocode with clear labels will earn method and attribute marks.
在编写伪代码时,不要忘记声明类、列出其属性并展示方法签名。即使你无法写出完美代码,结构清晰、标签明确的伪代码也能获得方法和属性方面的分数。
Published by TutorHao | Computer Science Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导