← 返回资讯屏轩教育 · 行业资讯

Object-Oriented Programming in Edexcel A-Level Computer Science | 爱德思A-Level计算机科学中的面向对象编程

2026-08-02 15:49 · 本文部分内容由AI辅助创作

📚 Object-Oriented Programming in Edexcel A-Level Computer Science | 爱德思A-Level计算机科学中的面向对象编程

Object-oriented programming (OOP) is a paradigm that structures code around objects rather than functions and logic. In Edexcel A-Level Computer Science, you are expected to understand the core principles of OOP, including classes, objects, encapsulation, inheritance, and polymorphism. This article provides a comprehensive revision guide, pairing key concepts with clear examples and exam-focused explanations.

面向对象编程(OOP)是一种围绕对象而非函数和逻辑来组织代码的编程范式。在爱德思A-Level计算机科学中,你需要理解OOP的核心原则,包括类、对象、封装、继承和多态。本文提供一份全面的复习指南,将关键概念与清晰的示例和考试重点讲解相结合。


1. Introduction to Object-Oriented Programming | 面向对象编程简介

Procedural programming focuses on writing procedures or functions that perform operations on data. In contrast, OOP bundles data and the methods that operate on that data into single units called objects. This mirrors how we perceive the real world — as collections of interacting objects, each with its own properties and behaviours.

过程式编程侧重于编写对数据执行操作的过程或函数。相比之下,OOP将数据以及操作这些数据的方法捆绑到称为对象的单个单元中。这反映了我们对现实世界的感知方式 —— 将其视为一个个相互交互的对象的集合,每个对象都有自己的属性和行为。

In the Edexcel specification, OOP is assessed in both Paper 1 (Principles of Computer Science) and the non-exam assessment (programming project). Understanding the theory is as important as being able to implement OOP concepts in a chosen high-level language such as Python, Java, or C#.

在爱德思考纲中,面向对象编程既会在试卷1(计算机科学原理)中考查,也会在非考试评估(编程项目)中体现。理解理论与能够在所选的高级语言(如Python、Java或C#)中实现OOP概念同等重要。


2. Classes and Objects: The Blueprint and Instances | 类与对象:蓝图与实例

A class is a template or blueprint that defines the attributes (data) and methods (behaviours) common to all objects of a certain kind. An object is a specific instance of a class, with actual values assigned to its attributes. For example, a Dog class might define attributes like name and breed, while each dog object (e.g., my_dog) holds concrete data such as 'Rex' and 'Labrador'.

类是一个模板或蓝图,定义了某一类对象共有的属性(数据)和方法(行为)。对象是类的一个具体实例,其属性被赋予了实际的值。例如,一个Dog类可能定义name和breed等属性,而每个狗对象(如my_dog)则持有'Rex'和'Labrador'这样的具体数据。

When you write my_dog = Dog('Rex', 'Labrador'), you are creating an instance of the Dog class. The act of creating an object from a class is called instantiation. The distinction between class and object is fundamental: the class exists at compile time, while objects exist at runtime.

当你编写my_dog = Dog('Rex', 'Labrador')时,你正在创建一个Dog类的实例。从类创建对象的过程称为实例化。类与对象之间的区别是根本性的:类在编译时存在,而对象在运行时存在。


3. Attributes and Methods: Data and Behaviour | 属性与方法:数据与行为

Attributes (also called properties or fields) store the state of an object. Methods are functions defined inside a class that describe the behaviours an object can perform. Attributes can be public (accessible from outside the class) or private (hidden from external access). In Python, a convention is to prefix private attributes with an underscore, e.g., _balance.

属性(也称为特性或字段)存储对象的状态。方法是在类内部定义的函数,描述了对象可以执行的行为。属性可以是公有的(可从类外部访问)或私有的(对外部访问隐藏)。在Python中,约定使用下划线前缀表示私有属性,例如_balance。

A method always takes at least one parameter — typically called self in Python or this in Java — which refers to the current object instance. For example, a BankAccount class might have a method def deposit(self, amount): to increase the balance. Understanding the difference between class attributes (shared by all instances) and instance attributes (unique to each object) is crucial for exam questions.

方法总是接受至少一个参数 —— 在Python中通常称为self,在Java中称为this —— 该参数引用当前的对象实例。例如,一个BankAccount类可能有一个方法def deposit(self, amount):来增加余额。理解类属性(由所有实例共享)和实例属性(每个对象唯一)之间的区别对于考试题目至关重要。


4. Encapsulation: Protecting Data with Access Modifiers | 封装:使用访问修饰符保护数据

Encapsulation is the mechanism of bundling data and methods into a single unit (the class) and restricting direct access to some of the object's components. It is implemented using access modifiers: public, private, and protected. In Python, true private attributes are not enforced, but name mangling (e.g., __secret) can be used to indicate strongly private intent.

封装是将数据和操作数据的方法捆绑到一个单元(类)中,并限制对对象某些组成部分的直接访问的机制。它通过访问修饰符实现:公有、私有和受保护。在Python中并不强制执行真正的私有属性,但可以使用名称改写(如__secret)来表示强烈的私有意图。

The main benefit of encapsulation is that it hides the internal representation of an object from the outside, allowing the internal implementation to change without affecting other parts of the program. For Edexcel, be able to explain why getter and setter methods are used to provide controlled access to private attributes.

封装的主要好处是它将对象的内部表示对外部隐藏,从而可以在不影响程序其他部分的情况下更改内部实现。对于爱德思考试,要能够解释为什么使用getter和setter方法来提供对私有属性的受控访问。


5. Constructors: Initialising Objects | 构造函数:初始化对象

A constructor is a special method that is automatically called when an object is created. It initialises the object's attributes and can enforce that an object starts in a valid state. In Python, the constructor method is __init__(self, ...). In Java, the constructor has the same name as the class.

构造函数是一种特殊方法,在创建对象时自动调用。它初始化对象的属性,并可确保对象以有效状态启动。在Python中,构造函数方法是__init__(self, ...)。在Java中,构造函数与类同名。

You can define multiple constructors through overloading (e.g., a default constructor with no parameters and a parameterised constructor). A class that does not explicitly define a constructor is usually given a default one by the compiler, but this default constructor initialises attributes to default values, which may not be suitable. Exam questions often ask you to write a constructor for a given class scenario.

你可以通过重载定义多个构造函数(例如,一个不带参数的默认构造函数和一个带参数的构造函数)。没有显式定义构造函数的类通常由编译器提供一个默认构造函数,但该默认构造函数将属性初始化为默认值,这可能不合适。考试题目经常要求你为给定的类场景编写一个构造函数。


6. Inheritance: Reusing and Extending Classes | 继承:重用和扩展类

Inheritance enables a new class (subclass or derived class) to take on the attributes and methods of an existing class (superclass or base class). This promotes code reuse and establishes a hierarchical relationship. For example, a Vehicle superclass might have a method move(), which is inherited by a Car subclass, but the Car can also add its own method honk().

继承使得一个新类(子类或派生类)能够获得现有类(超类或基类)的属性和方法。这促进了代码重用,并建立了层次关系。例如,一个Vehicle超类可能有一个move()方法,该方法被Car子类继承,但Car类还可以添加自己的honk()方法。

In Edexcel, you must be able to interpret and write class definitions that extend base classes, including the correct syntax for specifying inheritance (e.g., class Car(Vehicle): in Python). You should also understand the concept of method overriding, where a subclass provides a specific implementation of a method already defined in the superclass.

在爱德思考试中,你必须能够解释和编写扩展基类的类定义,包括指定继承的正确语法(例如Python中的class Car(Vehicle):)。你还应该理解方法重写的概念,即子类提供对超类中已定义方法的具体实现。


广告 / Sponsored

7. Polymorphism: Many Forms of Methods | 多态:方法的多重形态

Polymorphism means 'many forms'. In OOP, it allows objects of different classes to be treated as objects of a common superclass, while still responding to the same method call in their own specific ways. This is achieved through method overriding and dynamic binding at runtime.

多态意为“多种形态”。在面向对象编程中,它允许不同类的对象被视为一个共同超类的对象,同时仍然以各自特定的方式响应相同的方法调用。这是通过方法重写和运行时的动态绑定来实现的。

For example, a Shape superclass might define a method draw(). Subclasses Circle, Rectangle, and Triangle each override draw() to render their own shape. A list of Shape objects can then be iterated over, and calling draw() on each will execute the appropriate overridden method. This is a powerful way to write flexible and extensible code.

例如,一个Shape超类可能定义一个draw()方法。子类Circle、Rectangle和Triangle各自重写draw()以渲染自己的形状。然后可以遍历一个Shape对象列表,对每个对象调用draw()将执行相应重写的方法。这是编写灵活、可扩展代码的强大方式。


8. Method Overloading vs Method Overriding | 方法重载与方法重写

These two terms are frequently confused. Overloading occurs when multiple methods in the same class share the same name but have different parameter lists (number, type, or order). Overriding occurs when a subclass redefines a method inherited from a superclass with the same signature to provide specialised behaviour.

这两个术语经常被混淆。重载发生在同一类中的多个方法具有相同名称但参数列表(数量、类型或顺序)不同的情况下。重写则发生在子类重新定义从超类继承的具有相同签名的方法,以提供特定行为的情况下。

Note that Python does not support method overloading in the traditional sense; you can achieve similar behaviour using default arguments or variable-length arguments. However, overloading is common in Java and C#. The Edexcel specification may expect you to distinguish between them in pseudocode or theory questions.

请注意,Python并不支持传统意义上的方法重载;你可以使用默认参数或可变长度参数来实现类似行为。然而,重载在Java和C#中很常见。爱德思考纲可能期望你在伪代码或理论题中区分它们。


9. Abstract Classes and Interfaces | 抽象类与接口

An abstract class is a class that cannot be instantiated on its own and is designed to be subclassed. It may contain abstract methods — methods without a body that must be implemented by concrete subclasses. In Python, the abc module provides the ABC class and the @abstractmethod decorator.

抽象类是一个不能独立实例化、被设计为用于子类化的类。它可以包含抽象方法 —— 没有方法体、必须由具体子类实现的方法。在Python中,abc模块提供了ABC类和@abstractmethod装饰器。

An interface (prominent in Java) is a contract that specifies what methods a class must implement, without providing any implementation. Python does not have a formal interface keyword, but abstract base classes with only abstract methods can serve the same purpose. Edexcel may reference interfaces, especially when discussing design patterns or UML at a high level.

接口(在Java中尤为突出)是一种契约,规定了类必须实现哪些方法,而不提供任何实现。Python没有正式的接口关键字,但仅包含抽象方法的抽象基类可以达到相同目的。爱德思可能会在较高层次讨论设计模式或UML时提及接口。


10. Relationships Between Classes: Association, Aggregation, Composition | 类之间的关系:关联、聚合、组合

Classes can be related in different ways. Association is a 'uses-a' relationship where one class knows about another. Aggregation is a 'has-a' relationship that implies a whole-part connection where the part can exist independently of the whole (e.g., a Department and a Professor). Composition is a stronger 'has-a' relationship where the part cannot exist without the whole (e.g., a House and a Room).

类可以以不同方式关联。关联是一种“使用”关系,其中一个类知道另一个类。聚合是一种“拥有”关系,表示整体与部分的关系,其中部分可以独立于整体存在(例如,Department和Professor)。组合是一种更强的“拥有”关系,其中部分不能脱离整体而存在(例如,House和Room)。

In exam scenarios, you might be asked to identify the type of relationship from a description or a UML class diagram. Understanding the semantics helps in designing maintainable software, as composition enforces tighter coupling and lifecycle management.

在考试场景中,你可能会被要求根据描述或UML类图识别关系类型。理解这些语义有助于设计可维护的软件,因为组合要求更紧密的耦合和生命周期管理。


11. UML Class Diagrams: Visual Representation | UML类图:可视化表示

Unified Modelling Language (UML) class diagrams are a standard way to visualise classes, their attributes, methods, and relationships. A class is drawn as a rectangle divided into three compartments: name, attributes, and methods. Visibility markers like + (public) and - (private) are added before attribute and method names.

统一建模语言(UML)类图是可视化类、其属性、方法及关系的标准方式。一个类被绘制为一个分为三格的长方形:名称、属性和方法。在属性和方法名称之前会添加可见性标记,如+(公有)和-(私有)。

Associations are shown as lines between classes, with multiplicity annotations (e.g., 1, 0..*) and navigability arrows. Inheritance is represented by a hollow triangle pointing to the superclass. Edexcel questions may provide a UML diagram and ask you to implement the class structure in code, or vice versa.

关联关系以类之间的线条表示,并带有重数注释(如1、0..*)和导航箭头。继承则用一个指向超类的空心三角形表示。爱德思题目可能会提供一个UML图并要求你用代码实现类结构,或者反过来。


12. Benefits of OOP and Exam Tips | 面向对象编程的优点与考试技巧

Key advantages of OOP include modularity (each object is self-contained), code reuse through inheritance, maintainability due to encapsulation, and flexibility via polymorphism. These benefits make OOP especially suited for large, complex software projects.

面向对象编程的主要优点包括模块化(每个对象是自包含的)、通过继承实现的代码重用、由于封装带来的可维护性,以及通过多态获得的灵活性。这些优点使得OOP特别适合大型、复杂的软件项目。

When answering exam questions, always use correct terminology: 'instantiate' rather than 'create', 'subclass' instead of 'child class'. If asked to write code, ensure proper indentation and show constructor and accessor methods where appropriate. In evaluation questions, be prepared to compare OOP with procedural programming, noting trade-offs like initial complexity versus long-term extensibility.

在回答考试问题时,务必使用正确的术语:使用“实例化”而非“创建”,使用“子类”而非“子类”(注意:这里原文是subclass vs child class,中文其实都翻译为子类,但英文术语要准确)。如果要求编写代码,请确保正确的缩进,并在适当的地方编写构造函数和访问器方法。在评估类题目中,要准备好将OOP与过程式编程进行比较,指出诸如初始复杂度与长期可扩展性之间的权衡等不同之处。

Practice reading and writing UML class diagrams, and be comfortable translating between diagram and code representations. Remember that the non-exam assessment heavily rewards a well-structured OOP approach, so apply these principles in your own project.

多加练习阅读和绘制UML类图,并能在图表表示和代码表示之间自如转换。请记住,非考试评估非常看重结构良好的面向对象方法,因此在你自己的项目中要应用这些原则。

Published by TutorHao | Computer Science Revision Series | aleveler.com

广告 / Sponsored
英文原版 Collins International Primary Computing 柯林斯国际小学计算机科学学生…
计算机 备考教辅 · 正版现货 · 与本文同科同级别
查看详情 / View on Taobao

更多咨询请联系16621398022(同微信)

备考资讯每日更新,关注不错过
考纲变化 · 科目分析 · 学习方法
扫码关注公众号 tutorhao
在微信里长按二维码识别关注
或截图后用「扫一扫 → 相册」选择本图
公众号:tutorhao