📚 Object-Oriented Programming for IGCSE AQA | 面向对象 考点精讲
Object-oriented programming (OOP) is a cornerstone of modern software development and a key topic in the IGCSE AQA Computer Science specification. Understanding classes, objects, encapsulation, inheritance and polymorphism is essential for both the written paper and the programming project. This article breaks down every core concept you need to master, with clear explanations, code examples and exam-focused insights.
面向对象编程(OOP)是现代软件开发的基石,也是 IGCSE AQA 计算机科学大纲中的关键主题。理解类、对象、封装、继承和多态对笔试题和编程项目都至关重要。本文将逐个拆解你需要掌握的核心概念,配以清晰的解释、代码示例和考点分析。
1. What Is Object-Oriented Programming? | 什么是面向对象编程?
Object-oriented programming is a paradigm that organises software design around data, or objects, rather than functions and logic. An object is a self-contained entity that contains both data in the form of attributes and behaviours in the form of methods. This approach models real-world entities, making code more intuitive, reusable and easier to maintain.
面向对象编程是一种围绕数据(即对象)而非函数和逻辑来组织软件设计的编程范式。对象是一个自包含的实体,它既包含属性形式的数据,也包含方法形式的行为。这种方法模仿现实世界中的事物,使代码更直观、更可复用,也更易于维护。
In contrast to procedural programming, where programs are built from procedures or subroutines, OOP bundles related properties and functions together. This encapsulation leads to better modularity and reduces complexity when solving large problems.
与面向过程编程(程序由一个个过程或子程序构建)相比,OOP 将相关的属性和函数捆绑在一起。这种封装带来了更好的模块化,并在解决大型问题时降低了复杂度。
2. Classes and Objects | 类与对象
A class is a blueprint or template that defines the attributes and methods common to all objects of a certain kind. An object is a specific instance of a class, built from that blueprint with actual values for its attributes. You can think of a class as the design for a car, and an object as a particular car manufactured from that design.
类是一个蓝图或模板,定义了某类对象共有的属性和方法。对象是类的一个具体实例,根据蓝图构建并拥有具体的属性值。你可以把类想象成汽车的图纸,而对象就是用该图纸制造出来的某一辆具体的车。
For example, a class Student might declare attributes such as name, yearGroup and methods such as submitHomework(). Each object created from this class will store its own name and year group, and can call the same methods.
例如,一个 Student 类可能声明 name、yearGroup 等属性,以及 submitHomework() 等方法。从该类创建的每个对象都会存储自己的姓名和年级,并可以调用相同的方法。
CLASS Student
PUBLIC name: STRING
PUBLIC yearGroup: INTEGER
PUBLIC PROCEDURE submitHomework()
OUTPUT name, " submitted homework"
ENDPROCEDURE
ENDCLASS
// Creating objects
firstStudent = NEW Student()
firstStudent.name = "Alice"
firstStudent.yearGroup = 10
firstStudent.submitHomework()
In the AQA pseudocode, NEW is used to instantiate an object. The dot notation (object.attribute or object.method()) is then used to access members.
在 AQA 伪代码中,NEW 用于实例化对象,点符号(对象.属性 或 对象.方法())则用来访问成员。
3. Attributes and Methods | 属性与方法
Attributes are variables that belong to an object and store its state. Methods are subroutines (procedures or functions) that define the object’s behaviour. Together they encapsulate the data and the operations that can be performed on it.
属性是属于对象的变量,用于存储对象的状态。方法是定义对象行为的子程序(过程或函数)。它们共同封装了数据及可在其上执行的操作。
Attributes can be of any data type – strings, integers, Booleans, arrays or even other objects. Methods may return a value (functions) or simply perform an action (procedures). In the exam, you must be able to identify and declare both public and private attributes and methods correctly.
属性可以是任意数据类型——字符串、整数、布尔值、数组,甚至其他对象。方法可以返回值(函数)或仅仅执行某个操作(过程)。考试中你必须能够正确识别和声明公有/私有的属性和方法。
- Attribute example:
PRIVATE balance: REAL - Method example:
PUBLIC FUNCTION getBalance() RETURNS REAL
中文要点:属性如 PRIVATE balance: REAL 定义账户余额,方法如 PUBLIC FUNCTION getBalance() RETURNS REAL 用于返回该余额。使用 PRIVATE 关键字可以限制外部直接访问。
4. Constructors | 构造方法
A constructor is a special method that is automatically called when a new object is instantiated. Its main purpose is to initialise the object’s attributes with appropriate starting values. In AQA pseudocode, the constructor is defined using the NEW procedure inside the class definition.
构造器是一种特殊方法,在实例化新对象时自动调用。它的主要作用是给对象的属性赋予合适的初始值。在 AQA 伪代码中,构造器通过在类定义内部使用 NEW 过程来定义。
CLASS BankAccount
PRIVATE accountNumber: STRING
PRIVATE balance: REAL
PUBLIC PROCEDURE NEW(accNo: STRING, startBalance: REAL)
accountNumber = accNo
balance = startBalance
ENDPROCEDURE
PUBLIC FUNCTION getBalance() RETURNS REAL
RETURN balance
ENDFUNCTION
ENDCLASS
myAccount = NEW BankAccount("12345", 500.00)
Constructors allow you to pass arguments when creating an object, ensuring the object starts in a valid state. If no constructor is written, some languages provide a default one, but AQA questions often require you to write explicit NEW procedures.
构造器允许你在创建对象时传递参数,确保对象以有效状态启动。如果不写构造器,有些语言会提供默认构造器,但 AQA 的题目常常要求你显式写出 NEW 过程。
Exam tip: Remember to include the constructor in your class diagram or pseudocode if the question asks for initial values. It frequently appears when modelling bank accounts, students or game characters.
考试技巧:如果题目要求设定初始值,务必在类图或伪代码中包含构造器。在模拟银行账户、学生或游戏角色时,构造器常常出现。
5. Encapsulation and Access Modifiers | 封装与访问修饰符
Encapsulation is the principle of bundling data and the methods that operate on that data within one unit, and restricting direct access to some of the object’s components. This is achieved using access modifiers such as PUBLIC and PRIVATE.
封装是把数据与操作这些数据的方法绑定在一个单元里,并限制对某些组成部分的直接访问。这通过使用访问修饰符(如 PUBLIC 和 PRIVATE)来实现。
Attributes should usually be declared as PRIVATE to prevent uncontrolled modification from outside the class. Instead, the class provides PUBLIC ‘getter’ and ‘setter’ methods to read and update the values safely. This allows validation, logging or any other side effects to be added without breaking external code.
属性通常应声明为 PRIVATE,以防止从类外部进行不受控制的修改。取而代之的是,类提供 PUBLIC 的“读取器”和“设置器”方法,用于安全地读取和更新数据。这样就能添加验证、日志记录或其他副作用,而不会破坏外部代码。
CLASS StudentRecord
PRIVATE examScore: INTEGER
PUBLIC PROCEDURE setExamScore(newScore: INTEGER)
IF newScore >= 0 AND newScore <= 100 THEN
examScore = newScore
ELSE
OUTPUT "Invalid score"
ENDIF
ENDPROCEDURE
PUBLIC FUNCTION getExamScore() RETURNS INTEGER
RETURN examScore
ENDFUNCTION
ENDCLASS
Encapsulation improves security, data integrity and maintainability. In the exam, you may be asked to explain why attributes should be private and to rewrite code so that it properly encapsulates data.
封装提高了安全性、数据完整性和可维护性。考试中,你可能被要求解释为什么属性应该设为私有,并改写代码以实现正确的数据封装。
6. Inheritance | 继承
Inheritance allows a new class (subclass or derived class) to acquire the attributes and methods of an existing class (superclass or base class). The subclass can then add its own specific features or override inherited behaviour. This promotes code reuse and establishes a natural hierarchy.
继承允许一个新类(子类或派生类)获取现有类(超类或基类)的属性和方法。子类然后可以添加自己特有的功能,或者重写继承来的行为。这促进了代码复用,并建立了自然的层次结构。
In AQA pseudocode, inheritance is represented by the keyword INHERITS. For example, a Vehicle superclass might have attributes make and model, and a Car subclass inherits those and adds numberOfDoors.
在 AQA 伪代码中,继承用关键字 INHERITS 表示。例如,超类 Vehicle 可能有属性 make 和 model,子类 Car 继承这些属性,并添加 numberOfDoors。
CLASS Vehicle
PUBLIC make: STRING
PUBLIC model: STRING
PUBLIC PROCEDURE NEW(mk: STRING, md: STRING)
make = mk
model = md
ENDPROCEDURE
ENDCLASS
CLASS Car INHERITS Vehicle
PUBLIC numberOfDoors: INTEGER
PUBLIC PROCEDURE NEW(mk: STRING, md: STRING, doors: INTEGER)
SUPER.NEW(mk, md) // Call superclass constructor
numberOfDoors = doors
ENDPROCEDURE
ENDCLASS
The keyword SUPER is used to refer to the superclass, enabling the subclass to call the superclass constructor or methods. Inheritance questions often appear in the context of animals, vehicles or electronic devices.
关键字 SUPER 用于引用超类,使子类能够调用超类的构造器或方法。继承类的题目经常出现在动物、交通工具或电子设备的上下文中。
7. Polymorphism | 多态
Polymorphism means 'many forms'. It allows objects of different classes to be treated as objects of a common superclass, while still responding to methods in their own specific way. The most common forms are overriding and overloading.
多态意为“多种形态”。它允许将不同类的对象当作共同超类的对象来处理,同时这些对象仍能以各自特有的方式响应方法调用。最常见的形式是重写(覆盖)和重载。
Method overriding occurs when a subclass provides its own version of a method that is already defined in the superclass. The version called depends on the object's actual class at runtime. Method overloading (less common in AQA pseudocode but assessed conceptually) is when multiple methods share the same name but differ in parameter lists.
方法重写发生在子类提供了自己的版本,该版本已在超类中定义。实际调用哪个版本取决于运行时对象的真实类。方法重载(在 AQA 伪代码中不常见,但概念上会考察)是指多个方法同名但参数列表不同。
CLASS Shape
PUBLIC PROCEDURE draw()
OUTPUT "Drawing a shape"
ENDPROCEDURE
ENDCLASS
CLASS Circle INHERITS Shape
PUBLIC PROCEDURE draw()
OUTPUT "Drawing a circle"
ENDPROCEDURE
ENDCLASS
// Polymorphic behaviour
myShape: Shape
myShape = NEW Circle()
myShape.draw() // Output: "Drawing a circle"
Even though the reference myShape is of type Shape, it holds a Circle object, so the overridden draw() in Circle is executed. This is a powerful exam topic — be ready to trace code and explain which method is invoked.
尽管引用变量 myShape 的类型是 Shape,但它持有一个 Circle 对象,因此执行的是 Circle 中重写的 draw() 方法。这是一个重要的考点——请准备好跟踪代码并解释具体调用了哪个方法。
8. OOP vs Procedural Programming | 面向对象与面向过程的对比
| Aspect | OOP | Procedural |
|---|---|---|
| Focus | Objects that contain data and methods | Functions and sequences of tasks |
| Data safety | High (encapsulation, private attributes) | Lower (data often global or shared) |
| Reusability | Inheritance enables extensive code reuse | Limited to function libraries |
| Modelling | Natural mapping to real-world entities | Task-oriented, less intuitive mapping |
The table highlights that OOP provides better abstraction, security and scalability, making it the dominant paradigm for large applications. However, procedural programming can be simpler for small, linear tasks. Exam questions may ask you to compare the two or justify the choice of paradigm.
上表凸显了 OOP 提供更好的抽象、安全性和可扩展性,使其成为大型应用程序的主流范式。不过,面向过程编程对于小型线性任务可能更简单。考试题可能要求你比较两者,或者说明选择某种范式的理由。
9. Advantages of OOP | 面向对象的优点
Why is OOP so widely used? Here are the key advantages you should be able to discuss in exam answers:
为什么 OOP 应用如此广泛?以下是你需要在考试答案中能够阐述的关键优点:
- Modularity: Programs are broken into independent objects, making development and testing easier. / 模块化:程序被分解为独立的对象,使开发和测试更容易。
- Reusability: Inheritance allows new classes to be built on existing ones, saving time and reducing errors. / 可复用性:继承允许在已有类的基础上构建新类,节省时间并减少错误。
- Maintainability: Changes to a specific class do not affect the whole system if interfaces remain consistent. / 可维护性:如果接口保持一致,对某个类的修改不会影响整个系统。
- Data hiding: Private attributes protect critical data from unintended modification. / 数据隐藏:私有属性保护关键数据免遭意外修改。
- Real-world modelling: Objects mirror tangible entities, improving design clarity. / 现实世界建模:对象映射有形实体,增进设计清晰度。
- Extensibility: Polymorphism and inheritance make it easy to add new features without rewriting existing code. / 可扩展性:多态和继承使得添加新功能而不必重写现有代码变得容易。
10. Common Exam Questions | 常见考题精析
IGCSE AQA papers frequently test OOP through a mix of short-answer theory and code tracing. Here are typical question styles and what you need to include:
IGCSE AQA 试卷常通过简答题和代码分析相结合的方式考查 OOP。以下是典型题型及答题要点:
Q: Explain the difference between a class and an object.
You must mention that a class is a blueprint/template, while an object is an instance created from that blueprint. Use a concrete analogy like a biscuit cutter (class) and a biscuit (object).
问:解释类和对象的区别。
你必须提到类是蓝图/模板,而对象是从该蓝图创建的实例。用一个具体的类比,如饼干模具(类)和饼干(对象)。
Q: Why should attributes be declared as private?
To enforce encapsulation, protect data integrity, and control access through getters/setters that can validate input.
问:为什么属性应该声明为私有?
为了实施封装、保护数据完整性,并通过可验证输入的读取器/设置器来控制访问。
Q: Give one advantage of using inheritance.
Code reuse – subclasses inherit common behaviour, eliminating duplication. Also, it supports polymorphic collections.
问:给出使用继承的一个优点。
代码复用——子类继承共同行为,消除重复。此外还能支持多态集合。
Q: Trace this pseudocode and state the output.
You will be given a class hierarchy with overridden methods. You need to identify the actual object type to determine which method is called. Look for NEW keywords and the type of the reference variable.
问:跟踪此伪代码并写出输出。
题目会给出一个带重写方法的类层次结构。你需要识别对象的实际类型以确定调用哪个方法。留意 NEW 关键字和引用变量的类型。
Always write precise terminology: 'encapsulation' not 'hiding stuff', 'instantiate' not 'create a copy'. Precise language gains marks.
始终使用准确的术语:说“封装”而不是“把东西藏起来”,说“实例化”而不是“复制一个”。精准的表述能赢得分数。
11. Key Terminology Summary | 关键术语总结
| Term (English / 中文) | Definition |
|---|---|
| Class / 类 | A blueprint defining attributes and methods for a set of objects. |
| Object / 对象 | An instance of a class with its own state. |
| Attribute / 属性 | A variable holding data within an object (e.g. name, age). |
| Method / 方法 | A subroutine (procedure or function) that defines object behaviour. |
| Constructor / 构造器 | Special method NEW that initialises attributes when an object is created. |
| Encapsulation / 封装 | Bundling data with methods and restricting direct access using PRIVATE. |
| Inheritance / 继承 | Mechanism where a subclass acquires properties of a superclass using INHERITS. |
| Polymorphism / 多态 | Ability of different objects to respond appropriately to the same method call, often via overriding. |
| Public / 公有 | Access modifier allowing access from any code outside the class. |
| Private / 私有 | Access modifier restricting access to within the class itself. |
Revise this table actively: cover the definitions and recall them, then try to give an example from the syllabus. Consistent use of these terms in your long-answer responses will demonstrate a high level of subject knowledge.
主动复习此表:遮住定义并回忆,然后尝试从大纲中举例。在长答案中持续使用这些术语将展示高水平的学科知识。
12. Final Tips for the OOP Section | OOP 部分备考提示
To maximise your marks in OOP questions, practise writing short pseudocode for class definitions, constructors, and inheritance hierarchies. Use past papers to get familiar with the AQA style – many questions require you to complete partially written classes or identify errors in an OOP design.
要在 OOP 题目中拿到最高分,请练习编写类定义、构造器和继承层次的简短伪代码。使用往年真题熟悉 AQA 风格——许多题目要求你补全部分编写的类,或找出 OOP 设计中的错误。
Always indicate access modifiers explicitly in your answers, and when explaining concepts, link back to the advantages (reusability, security, maintainability). Connecting theory to practical benefits shows deeper understanding.
在答案中始终明确写出访问修饰符,在解释概念时,要联系回优点(可复用性、安全性、可维护性)。将理论联系到实际益处能展现更深层次的理解。
Finally, remember that OOP principles appear not only in the theory paper but also in the non-examined assessment (NEA) where you design your own program. Demonstrating encapsulation and inheritance in your project can elevate your marks significantly.
最后,请记住 OOP 原则不仅出现在理论试卷中,也会在非考试评估(NEA)的项目设计中涉及。在你的项目中展示封装和继承能显著提升分数。
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课程辅导,国外大学本科硕士研究生博士课程论文辅导