📚 Object-Oriented Programming | 面向对象编程
Object-oriented programming (OOP) is a paradigm that organises software design around data, or objects, rather than functions and logic. In the Edexcel A-Level Computer Science specification, understanding OOP is essential for tackling Paper 2 questions on programming techniques and computational thinking. This article breaks down the core principles and terminology you need to master.
面向对象编程是一种围绕数据(即对象)而非函数和逻辑来组织软件设计的范式。在 Edexcel A-Level 计算机科学大纲中,理解面向对象编程对于攻克 Paper 2 中关于编程技术和计算思维的题目至关重要。本文将分解你需要掌握的核心理念和术语。
1. Introduction to Programming Paradigms | 编程范式简介
A programming paradigm is a fundamental style of computer programming. The main paradigms you encounter at A-Level are procedural, object-oriented, and declarative. While procedural programming structures code as a sequence of instructions operating on data, OOP bundles data and the methods that act on that data together into objects.
编程范式是一种基本的计算机编程风格。在 A-Level 阶段你会遇到的主要范式有过程式、面向对象式和声明式。过程式编程将代码构造为一条条操作数据的指令序列,而面向对象编程则将数据及作用于数据的方法捆绑在一起形成对象。
Edexcel’s specification expects you to compare these paradigms. In procedural code, data is often separate from the functions that change it, making it harder to maintain large systems. OOP addresses this by modelling real-world entities as self-contained units.
Edexcel 大纲要求你比较这些范式。在过程式代码中,数据通常与改变数据的函数分离,使得大型系统更难维护。面向对象编程通过将现实世界实体建模为自包含单元来解决这一问题。
Common OOP languages include Python, Java, and C++. In your exam, pseudocode or Python-style syntax may be used to illustrate OOP concepts, so you should feel comfortable reading class definitions and method calls.
常见的面向对象语言包括 Python、Java 和 C++。在考试中,可能会使用伪代码或 Python 风格语法来说明面向对象概念,因此你应该能轻松阅读类定义和方法调用。
2. The Object-Oriented Approach | 面向对象的方法
The central idea of OOP is that a program is a collection of interacting objects. Each object is an instance of a class and can receive messages, process data, and send messages to other objects. This approach helps manage complexity by breaking a system into manageable, reusable components.
面向对象编程的核心思想是:一个程序是相互交互的对象的集合。每个对象都是某个类的实例,可以接收消息、处理数据并向其他对象发送消息。这种方法通过将系统分解为可管理、可重用的组件来帮助控制复杂性。
Rather than focusing on the step-by-step flow of control, OOP emphasises the relationships between objects. An object encapsulates its state (attributes) and behaviour (methods). State represents what an object knows, and behaviour represents what an object can do.
面向对象编程不侧重于逐步的控制流,而是强调对象之间的关系。一个对象封装了其状态(属性)和行为(方法)。状态代表对象所知道的信息,行为代表对象能做什么。
This paradigm closely mirrors how we perceive the real world. For example, a ‘Car’ object might have attributes like colour and speed, and methods like accelerate() and brake(). In exam scenarios, you may be asked to design a class based on a description, so practice translating requirements into attributes and methods.
这种范式非常贴近我们感知现实世界的方式。例如,一个“汽车”对象可能具有颜色和速度等属性,以及 accelerate() 和 brake() 等方法。在考试情境中,你可能会被要求根据描述设计一个类,因此要练习将需求转化为属性和方法。
3. Classes and Objects | 类与对象
A class is a template or blueprint that defines the structure and behaviour common to a set of objects. It specifies what attributes objects of that type will have and what methods they will provide. You can think of a class as a cookie cutter, and objects as the cookies made from it.
类是定义了某一组对象共有的结构和行为的模板或蓝图。它指定了该类型的对象将具有哪些属性以及提供哪些方法。你可以把类比作饼干模具,而对象就是用模具制作出来的饼干。
An object is a concrete instance of a class. When you create an object, you allocate memory for its attributes and you can then call its methods. For instance, in Python, my_car = Car('red', 60) creates an object of class Car. Each object has its own copy of attribute values, so two Car objects can have different colours.
对象是类的具体实例。当你创建一个对象时,你会为它的属性分配内存,然后你可以调用它的方法。例如,在 Python 中,my_car = Car('red', 60) 创建了 Car 类的一个对象。每个对象都有自己的一份属性值副本,因此两个 Car 对象可以有不同的颜色。
In OOP design, identifying the right classes is crucial. During analysis, you look for nouns in the problem description to suggest classes, and verbs to suggest methods. This technique, often called noun-verb analysis, is a common exam task.
在面向对象设计中,识别正确的类至关重要。在分析阶段,你可以在问题描述中寻找名词来提示类,寻找动词来提示方法。这种技巧通常称为名词–动词分析,是考试中的常见任务。
4. Attributes and Methods | 属性与方法
Attributes (also called properties or fields) are variables that belong to an object. They store the state of the object. Methods are functions defined inside a class that describe the behaviours of an object. In a BankAccount class, for example, the balance is an attribute, while deposit(amount) and withdraw(amount) are methods.
属性(也称为属性或字段)是属于对象的变量,它们存储对象的状态。方法是定义在类内部、描述对象行为的函数。例如,在一个 BankAccount 类中,余额是一个属性,而 deposit(amount) 和 withdraw(amount) 是方法。
Attributes are usually accessed using dot notation, such as account.balance. However, good OOP practice often restricts direct access to attributes from outside the class—this is encapsulation. Instead, you provide getter and setter methods to read and modify attribute values safely.
属性通常使用点标记法访问,如 account.balance。不过,良好的面向对象实践通常限制从类外部直接访问属性——这就是封装。取而代之的是,你提供 getter 和 setter 方法来安全地读取和修改属性值。
Methods can be public or private depending on visibility. In exam pseudocode, a minus sign (–) before a method name sometimes indicates a private method. You should understand that private methods can only be called from within the same class.
方法根据可见性可以是公有的或私有的。在考试伪代码中,方法名前有时会使用减号(–)来表示私有方法。你应该理解私有方法只能在同一个类内部被调用。
5. Encapsulation and Data Hiding | 封装与数据隐藏
Encapsulation is the bundling of data with the methods that operate on that data. It restricts direct access to some of an object’s components, which is a means of preventing accidental interference and misuse of the data. This principle is sometimes called data hiding.
封装是将数据与操作该数据的方法绑定在一起。它限制了对某个对象某些组件的直接访问,这是防止数据被意外干扰和误用的一种手段。这个原则有时被称为数据隐藏。
Encapsulation helps make a class self-contained. The internal workings of a class can be changed without affecting code that uses the class, as long as the public interface (the methods that can be called from outside) remains the same. This separation between interface and implementation is a powerful design technique.
封装有助于使类变得自包含。只要公有接口(可以从外部调用的方法)保持不变,类的内部工作方式就可以被修改而不影响使用该类的代码。接口与实现之间的这种分离是一种强大的设计技巧。
In A-Level questions, you may be asked to explain how encapsulation improves program maintainability. By hiding data, you ensure that the state of an object can only be changed in controlled ways—through methods that may validate input or maintain consistency.
在 A-Level 问题中,你可能会被要求解释封装如何提高程序的可维护性。通过隐藏数据,你确保对象的状态只能以受控的方式改变——通过可能验证输入或保持一致性的方法。
6. Inheritance: Extending Classes | 继承:类的扩展
Inheritance allows a new class (subclass or derived class) to be based on an existing class (superclass or base class). The subclass inherits attributes and methods from the superclass, and can also add new ones or override existing ones. This models an ‘is-a’ relationship.
继承允许一个新类(子类或派生类)基于一个已有的类(超类或基类)。子类继承超类的属性和方法,还可以新增属性和方法,或重写已有的。这模拟了一种“是一个”的关系。
For example, a Vehicle superclass might have attributes like speed and a method move(). A Car subclass would inherit these but also add attributes like numberOfDoors. Inheritance promotes code reuse and logical hierarchy.
例如,一个 Vehicle 超类可能具有 speed 属性和 move() 方法。一个 Car 子类会继承这些内容,但也会添加 numberOfDoors 等属性。继承促进了代码重用和逻辑分层。
In your exam, you may see UML-like diagrams showing inheritance with an arrow from subclass to superclass. You should be able to interpret these diagrams and write code that demonstrates inheritance, understanding that private members of a superclass are not directly accessible in the subclass.
在考试中,你可能会看到类似 UML 的图示,用从子类指向超类的箭头表示继承。你应该能够解读这些图并编写展示继承的代码,同时理解超类的私有成员在子类中不能直接访问。
7. Polymorphism: Many Forms | 多态:多种形态
Polymorphism, from Greek meaning ‘many forms’, allows objects of different classes to be treated as objects of a common superclass. The most common use is when a subclass overrides a method of its superclass with its own implementation, and the correct version is chosen at runtime based on the actual object type.
多态,源自希腊语,意为“多种形态”,允许把不同类的对象当作共同的超类对象来对待。最常见的用法是,当子类用自己的实现重写了超类的方法,而运行时根据实际对象类型来选择正确的版本。
For instance, a method draw() could be defined in a Shape superclass and overridden in Circle and Square subclasses. A list of Shape objects could contain both Circle and Square instances, and calling draw() on each would invoke the appropriate overridden method.
例如,可以在 Shape 超类中定义 draw() 方法,并在 Circle 和 Square 子类中重写它。一个 Shape 对象列表可以同时包含 Circle 和 Square 实例,对每个对象调用 draw() 时将调用相应重写后的方法。
Polymorphism makes code more flexible and extensible. You can introduce new subclasses without changing existing code that works with the superclass type. Exam questions often ask you to identify or give an example of polymorphism.
多态使代码更加灵活、可扩展。你可以在不改变使用超类类型现有代码的情况下引入新的子类。考试题目常常要求你识别或举例说明多态。
8. Abstract Classes and Interfaces | 抽象类与接口
An abstract class is a class that cannot be instantiated directly and is designed to be inherited. It may contain abstract methods—methods without implementation that subclasses must override. Abstract classes define a common protocol for a group of related subclasses.
抽象类是不能直接实例化、专门设计用来被继承的类。它可以包含抽象方法——即没有实现、要求子类必须重写的方法。抽象类为一组相关的子类定义了一个共同的协议。
In many OOP languages, an interface is a collection of abstract methods and constants. A class that implements an interface must provide concrete implementations for all its methods. Unlike inheritance, which forms an ‘is-a’ hierarchy, interfaces represent a ‘can-do’ contract.
在许多面向对象语言中,接口是一组抽象方法和常量的集合。实现某个接口的类必须为接口的所有方法提供具体实现。与形成“是一个”层级的继承不同,接口代表了一种“能做”的契约。
For Edexcel A-Level, you do not need to master interface syntax in a specific language, but you should understand the concepts of abstract classes and the idea of defining a set of methods that must be implemented, as this supports polymorphism and loose coupling.
对于 Edexcel A-Level,你不需要掌握特定语言中的接口语法,但应该理解抽象类以及定义一组必须实现的方法的概念,因为这支持多态和松耦合。
9. Constructors and Destructors | 构造函数与析构函数
A constructor is a special method called automatically when an object is created. It usually initialises the object’s attributes with default or passed-in values. In Python, the constructor is __init__(self, ...); in many other languages, it has the same name as the class.
构造函数是一种特殊方法,在创建对象时自动调用。它通常用默认值或者传入的值来初始化对象的属性。在 Python 中,构造函数是 __init__(self, ...);在许多其他语言中,它与类同名。
Constructors ensure that an object starts its life in a valid state. Without a constructor, attributes would have to be set after creation, which could lead to incomplete or inconsistent objects. Overloading constructors (providing multiple versions with different parameters) is possible in some languages but not directly in Python.
构造函数确保对象在其生命周期开始时处于有效状态。如果没有构造函数,属性就必须在创建之后设置,这可能导致对象不完整或不一致。某些语言允许重载构造函数(提供多个不同参数版本的构造函数),但在 Python 中不能直接这样做。
A destructor, in contrast, is called when an object is destroyed, used to perform cleanup tasks such as closing files or releasing resources. Although not a major A-Level focus, understanding that destructors exist helps complete the life-cycle picture.
与此相对,析构函数在对象被销毁时调用,用于执行清理任务,如关闭文件或释放资源。虽然这不是 A-Level 的重点,但了解析构函数的存在有助于完善生命周期的图景。
10. Overloading and Overriding | 重载与重写
Method overloading means having multiple methods with the same name but different parameter lists within the same class. The compiler or interpreter chooses which version to call based on the arguments passed. This is a form of compile-time polymorphism.
方法重载意味着在同一个类中拥有多个同名但参数列表不同的方法。编译器或解释器根据传入的参数来选择调用哪一个版本。这是编译时多态的一种形式。
Method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its superclass. The subclass version has the same name, return type, and parameters. This is runtime polymorphism and is central to achieving dynamic behaviour.
方法重写发生在子类为其超类中已定义的方法提供特定实现的时候。子类版本具有相同的名称、返回类型和参数。这是运行时多态,对实现动态行为至关重要。
Edexcel A-Level questions may ask you to distinguish between overloading and overriding. Remember: overloading is about multiple methods in the same class with different signatures; overriding is about redefining a superclass method in a subclass.
Edexcel A-Level 题目可能会要求你区分重载和重写。请记住:重载是在同一个类中拥有多个签名不同的方法;重写是在子类中重新定义超类的方法。
11. Advantages and Disadvantages of OOP | 面向对象编程的优缺点
OOP offers several advantages. It promotes code reusability through inheritance, makes programs more modular and easier to maintain, and models real-world systems naturally. Encapsulation improves security and data integrity. Polymorphism allows flexible and extensible designs.
面向对象编程具有多个优点。它通过继承促进代码重用,使程序更具模块化且易于维护,并能自然地模拟现实系统。封装提高了安全性与数据完整性。多态允许灵活且可扩展的设计。
However, OOP is not without drawbacks. OOP programs can be less efficient due to the overhead of dynamic dispatch and object creation. The learning curve is steeper, and poor class design can lead to overly complex hierarchies. In some cases, procedural or functional approaches are simpler and more suitable.
不过,面向对象编程也并非没有缺点。由于动态调度和对象创建的开销,面向对象程序的效率可能较低。学习曲线更陡峭,糟糕的类设计可能导致过于复杂的层次结构。在某些情况下,过程式或函数式方法更简单、更合适。
Exam questions often require you to evaluate the choice of paradigm for a given scenario. You should be able to argue both for and against using OOP, considering factors like project size, need for reuse, and team expertise.
考试题目常常要求你针对给定情景评估范式的选择。你应该能够从正反两方面论证是否使用面向对象编程,并考虑项目规模、重用需求以及团队专业知识等因素。
12. OOP in A-Level Exams | A-Level 考试中的面向对象编程
For the Edexcel A-Level Computer Science Paper 2, you must be able to read, understand, and write class definitions using pseudocode or a high-level language. You may be asked to trace the behaviour of OOP code, identify classes and relationships from a scenario, or complete a partially written class.
在 Edexcel A-Level 计算机科学 Paper 2 中,你必须能够使用伪代码或高级语言阅读、理解并编写类定义。你可能需要追踪面向对象代码的行为,从情景中识别类和关系,或补全一个部分编写的类。
Typical command words include ‘design’, ‘explain’, ‘compare’, and ‘implement’. When answering, always relate your points back to the four pillars of OOP: encapsulation, inheritance, polymorphism, and abstraction. Using the correct terminology is key to gaining marks.
典型的指令词包括“设计”、“解释”、“比较”和“实现”。回答时,务必将你的论点联系回面向对象编程的四大支柱:封装、继承、多态和抽象。使用正确的术语是得分的关键。
Practice with past papers and the ActiveLearn activities will build your confidence. Remember that clarity and precision in your answers—whether describing a class interface or justifying the use of inheritance—will set you apart in the exam.
通过历年真题和 ActiveLearn 活动进行练习,将增强你的信心。请记住,回答时的清晰与准确——无论是描述类接口,还是论证继承的合理性——将使你在考试中脱颖而出。
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课程辅导,国外大学本科硕士研究生博士课程论文辅导