📚 A-Level Computer Science: Object-Oriented Programming Key Concepts | A-Level 计算机:面向对象考点精讲
Object-Oriented Programming (OOP) is a fundamental paradigm in A-Level Computer Science, shaping how students design, structure, and reason about software. This revision guide distils the essential concepts—classes, objects, encapsulation, inheritance, polymorphism, and UML modelling—into a concise yet comprehensive overview, aligned with A-Level assessment objectives. Each section pairs English explanations with Chinese translations to support bilingual learners.
面向对象编程(OOP)是 A-Level 计算机科学中的核心范式,它塑造了学生设计、构建和推理软件的方式。本复习指南将类、对象、封装、继承、多态和 UML 建模等重要概念浓缩为简洁而全面的概述,紧扣 A-Level 评估目标。每个小节均配有中英文解释,以帮助双语学习者。
1. What is Object-Oriented Programming? | 什么是面向对象编程?
Object-Oriented Programming organises code into reusable blueprints called classes, which define the properties (attributes) and behaviours (methods) of objects. Instead of separating data and functions, OOP binds them together, modelling real-world entities for better modularity and maintainability.
面向对象编程将代码组织成称为类的可重用蓝图,类定义了对象的属性(特性)和行为(方法)。OOP 并不将数据和函数分离,而是将它们绑定在一起,对现实世界的实体进行建模,从而获得更好的模块化和可维护性。
The four pillars of OOP are encapsulation, inheritance, polymorphism, and abstraction. These principles enable developers to manage complexity, promote code reuse, and design systems that are easier to extend.
OOP 的四大支柱是封装、继承、多态和抽象。这些原则使开发人员能够管理复杂性、促进代码重用,并设计出更易于扩展的系统。
2. Classes and Objects | 类与对象
A class is a template or blueprint that specifies the attributes and methods common to all objects of a certain kind. For example, a Car class might have attributes like colour, model, and speed, and methods such as accelerate() and brake().
类是模板或蓝图,规定了某一类所有对象共有的属性和方法。例如,一个 Car 类可能具有 colour、model 和 speed 等属性,以及 accelerate() 和 brake() 等方法。
An object is an instance of a class, occupying memory and possessing its own state. You can create multiple distinct objects from the same class, each holding different data.
对象是类的实例,占用内存并拥有自己的状态。你可以从同一个类创建多个不同的对象,每个对象持有不同的数据。
In pseudocode and Python (common in A-Level), instantiation follows a straightforward pattern:
在 A-Level 常用的伪代码和 Python 中,实例化遵循简单的模式:
myCar ← new Car(“red”, “SUV”) // pseudocode
my_car = Car(“red”, “SUV”) # Python
3. Attributes and Methods | 属性与方法
Attributes store the state of an object. They can be public, private, or protected, depending on the language. Private attributes (prefixed with __ in Python, or - in UML) should only be accessed via getter and setter methods to preserve encapsulation.
属性存储对象的状态。根据语言的不同,它们可以是公共的、私有的或受保护的。私有属性(在 Python 中以 __ 为前缀,在 UML 中以 - 为前缀)应仅通过 getter 和 setter 方法访问,以保持封装性。
Methods define the behaviours of an object. They often operate on the object’s attributes. A method can be an accessor (getter), a mutator (setter), or a general utility function. In A-Level exams, you must be able to distinguish between these and write correct signatures.
方法定义了对象的行为,通常对对象的属性进行操作。方法可以是访问器(getter)、修改器(setter)或通用功能函数。在 A-Level 考试中,你必须能够区分它们并写出正确的签名。
Consider a BankAccount class:
考虑一个 BankAccount 类:
| Attribute/Method | Description |
|---|---|
| balance : REAL | Current funds (private) |
| getBalance() : REAL | Returns the balance |
| deposit(amount : REAL) | Adds amount to balance |
4. Constructors | 构造方法
A constructor is a special method invoked automatically when an object is instantiated. It initialises the object’s attributes and ensures the object starts in a valid state. In Python, the constructor is __init__(), while in Java-like pseudocode it bears the same name as the class.
构造方法是对象实例化时自动调用的特殊方法。它初始化对象的属性,确保对象以有效状态启动。在 Python 中,构造函数是 __init__(),而在类似 Java 的伪代码中,它与类同名。
Constructors can be overloaded to provide different ways of creating objects—e.g., a default constructor with no parameters and a parameterised constructor that sets initial values. A-Level questions often ask you to write a constructor from a UML diagram or a description.
构造函数可以重载,以提供不同的对象创建方式——例如,无参数的默认构造函数和设置初始值的有参构造函数。A-Level 题目经常要求你根据 UML 图或描述编写构造函数。
public class Car:
public Car(make, model):
self.make = make
self.model = model
5. Encapsulation and Access Modifiers | 封装与访问修饰符
Encapsulation is the mechanism of hiding the internal state of an object and requiring all interaction to occur through well-defined interfaces (methods). This protects data integrity and reduces coupling between components.
封装是隐藏对象内部状态并要求所有交互通过明确定义的接口(方法)进行的机制。这能保护数据完整性并降低组件之间的耦合度。
Access modifiers control visibility: public (+ in UML) is accessible from anywhere; private (- in UML) is only within the class; protected (# in UML) allows access from subclasses. In A-Level pseudocode, you may use keywords like PRIVATE and PUBLIC.
访问修饰符控制可见性:public(UML 中用 +)可从任何地方访问;private(UML 中用 -)仅在类内可访问;protected(UML 中用 #)允许子类访问。在 A-Level 伪代码中,你可以使用 PRIVATE 和 PUBLIC 等关键字。
A typical exam technique is to identify where encapsulation is broken and suggest improvements, such as making attributes private and adding getters/setters with validation.
一种典型的考试技巧是识别封装被破坏的地方并提出改进建议,例如将属性设为私有并添加带有验证的 getter/setter。
6. Inheritance | 继承
Inheritance allows a new class (subclass or child) to derive attributes and methods from an existing class (superclass or parent). It promotes code reuse and establishes an “is-a” relationship. For instance, a Dog class might inherit from Animal, gaining name and eat() while adding its own bark() method.
继承允许新类(子类或孩子类)从现有类(超类或父类)派生属性和方法。它促进代码重用并建立“是一种”的关系。例如,Dog 类可以从 Animal 继承,获得 name 和 eat(),同时添加自己的 bark() 方法。
Multiple inheritance (a class inheriting from more than one parent) is supported in some languages like Python but not in Java. A-Level specifications typically discuss single inheritance and the problems of the diamond dependency.
多继承(一个类从多个父类继承)在 Python 等语言中支持,但在 Java 中不支持。A-Level 规范通常讨论单继承以及菱形依赖问题。
In pseudocode, inheritance is often shown with the INHERITS keyword:
在伪代码中,继承通常用 INHERITS 关键字表示:
class Dog INHERITS Animal:
METHOD bark():
OUTPUT “Woof!”
7. Polymorphism | 多态
Polymorphism means “many forms.” It allows objects of different classes to respond to the same method call in ways specific to their class. The most common type in A-Level is overriding, where a subclass provides a specific implementation of a method already defined in its superclass.
多态意味着“多种形态”。它允许不同类的对象以各自特有的方式响应相同的方法调用。A-Level 中最常见的类型是重写,即子类为其超类中已定义的方法提供特定的实现。
Consider a superclass Shape with a method area(). Subclasses Circle and Rectangle override area() with their specific formulas. A list of Shape objects can call area() without knowing the exact class, enabling flexible and extendable code.
考虑一个超类 Shape,它有方法 area()。子类 Circle 和 Rectangle 用各自的公式重写 area()。一组 Shape 对象可以在不知道具体类的情况下调用 area(),从而实现灵活且可扩展的代码。
Dynamic dispatch is the runtime mechanism that selects the correct method version. A-Level exam questions often ask you to trace polymorphic method calls or explain how polymorphism improves maintainability.
动态分派是在运行时选择正确方法版本的机制。A-Level 考试题目经常要求你追踪多态方法调用或解释多态如何提高可维护性。
8. Abstract Classes and Interfaces | 抽象类与接口
An abstract class cannot be instantiated directly; it serves as a base for other classes, providing common functionality while forcing subclasses to implement specific behaviours. Abstract methods have no body and must be overridden.
抽象类不能直接实例化;它作为其他类的基类,提供通用功能,同时强制子类实现特定的行为。抽象方法没有主体,必须被重写。
An interface defines a contract of method signatures without any implementation. Classes that implement an interface must provide code for all its methods. Interfaces allow a form of multiple inheritance of behaviour, avoiding the diamond problem.
接口定义了一组方法签名的合约,没有任何实现。实现接口的类必须为其所有方法提供代码。接口允许一种行为的多继承形式,避免了菱形问题。
In A-Level pseudocode, abstract classes may be declared with ABSTRACT CLASS and interfaces with INTERFACE. The distinction is frequently tested: “Why use an interface rather than an abstract class?” (e.g., when unrelated classes need to share a capability).
在 A-Level 伪代码中,抽象类可以用 ABSTRACT CLASS 声明,接口用 INTERFACE 声明。两者的区别经常被考查:“为什么使用接口而不是抽象类?”(例如,当不相关的类需要共享某种能力时)。
9. UML Class Diagrams | UML 类图
Unified Modelling Language (UML) class diagrams visually represent the static structure of an OOP system. A class box contains three compartments: name, attributes, and methods. Visibility symbols (+, -, #) precede each member.
统一建模语言(UML)类图直观地表示 OOP 系统的静态结构。类框包含三个部分:名称、属性和方法。可见性符号(+、-、#)放在每个成员之前。
Relationships include inheritance (solid line with hollow triangle pointing to parent), association (solid line), aggregation (hollow diamond), and composition (filled diamond). Multiplicity notations like 1, 0..*, 1..* indicate how many objects are involved.
关系包括继承(实线带空心三角形指向父类)、关联(实线)、聚合(空心菱形)和组合(实心菱形)。重数符号如 1、0..*、1..* 表示涉及的对象数量。
Exam questions often require drawing or interpreting a UML diagram from a scenario. For example:
考试题目常要求根据场景绘制或解释 UML 图。例如:
| Class | Attributes | Methods |
|---|---|---|
| Library | – name : STRING – address : STRING |
+ addBook(b: Book) + search(title: STRING) : Book |
| Book | – isbn : STRING – title : STRING |
+ getISBN() : STRING |
10. Association, Aggregation, and Composition | 关联、聚合与组合
These three relationships express how objects are connected. Association is a general “uses-a” relationship, where objects are aware of each other but can exist independently. Aggregation is a “has-a” relationship where the whole contains parts that can exist separately (e.g., a Team aggregates Player objects). Composition is a strong “part-of” relationship where parts cannot exist without the whole (e.g., a House is composed of Room objects; if the house is destroyed, the rooms cease to exist conceptually).
这三种关系表达了对象之间的连接方式。关联是一种一般的“使用”关系,对象之间相互知晓但可以独立存在。聚合是一种“拥有”关系,整体包含的部分可以独立存在(例如,Team 聚合了 Player 对象)。组合是一种强的“部分-整体”关系,其中部分不能脱离整体而存在(例如,House 由 Room 对象组成;如果房子被摧毁,房间在概念上也不复存在)。
In UML, association is a plain line, aggregation has a hollow diamond at the whole end, and composition has a filled diamond. Multiplicity clarifies the numbers, e.g., a Car has exactly one Engine (composition), while an Engine may belong to zero or one Car at a time.
在 UML 中,关联是一条普通线,聚合在整体端有一个空心菱形,组合有一个实心菱形。多重性明确数量关系,例如,一辆 Car 恰好有一个 Engine(组合),而一个 Engine 在某一时刻可能属于零辆或一辆车。
11. Overriding vs Overloading | 重写与重载
These two concepts are often confused. Overriding occurs when a subclass provides a different implementation for a method inherited from its superclass. The method signature (name and parameters) remains the same. It enables runtime polymorphism.
这两个概念经常被混淆。重写发生在子类为其从超类继承的方法提供不同实现时。方法签名(名称和参数)保持不变。它实现了运行时多态。
Overloading is defining multiple methods in the same class with the same name but different parameter lists (type, number, or order). It is resolved at compile time. For example, a print() method might accept a STRING or an INTEGER.
重载是在同一个类中定义多个同名但参数列表(类型、数量或顺序)不同的方法。它在编译时解析。例如,print() 方法可以接受一个 STRING 或一个 INTEGER。
A typical A-Level question asks: “Explain the difference between overriding and overloading, providing an example of each.” Mastering this distinction is essential for top marks.
典型的 A-Level 题目会问:“解释重写和重载的区别,并各举一个例子。”掌握这一区别对于获得高分至关重要。
12. OOP Design Principles | 面向对象设计原则
Beyond syntax, A-Level syllabuses increasingly emphasise design quality. The Single Responsibility Principle states that a class should have only one reason to change. Encapsulation ensures that changes stay local. Liskov substitution requires that subclasses can stand in for their superclasses without breaking the program.
除了语法之外,A-Level 课程大纲越来越强调设计质量。单一职责原则指出,一个类应该只有一个改变的理由。封装确保更改保持在局部。里氏代换要求子类可以替代其超类而不会破坏程序。
Using UML, inheritance, and interfaces appropriately leads to loose coupling and high cohesion, which are marks of well-engineered software. Students should practice refactoring a given design to eliminate violations of these principles.
恰当地使用 UML、继承和接口可以带来低耦合和高内聚,这是设计良好的软件的标志。学生应该练习重构给定的设计以消除对这些原则的违反。
When you encounter a scenario-based question, identify the entities, their relationships, and the most suitable OOP constructs. Justify your choices with reference to encapsulation, reusability, and maintainability.
当你遇到基于场景的问题时,识别实体、它们的关系以及最合适的 OOP 构造。引用封装、可重用性和可维护性来证明你的选择。
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课程辅导,国外大学本科硕士研究生博士课程论文辅导