📚 Object-Oriented Programming (OOP) Essentials for Edexcel A-Level | 面向对象编程核心(Edexcel A-Level)
Object-oriented programming is a central paradigm in the Edexcel A-Level Programming unit, requiring learners to move beyond linear scripts and design software using interacting objects. This article revises the core OOP principles, common syntax patterns and the assessment style you can expect in the exam.
面向对象编程是 Edexcel A-Level 编程单元的核心范型,要求学习者超越线性脚本,使用相互协作的对象来设计软件。本文复习面向对象的核心原则、常见语法模式以及考试中可能遇到的题型。
1. Course Context and Assessment | 课程背景与考核方式
In the Edexcel A-Level Programming specification (Paper 2 or Unit 2 depending on your pathway), OOP is assessed through short-answer questions, trace-table tasks, and extended responses that ask you to design or evaluate class hierarchies.
在 Edexcel A-Level 编程大纲中(试卷 2 或单元 2,取决于课程路径),面向对象编程通过简答题、跟踪表任务和扩展回答来评估,要求你设计或评估类层次结构。
Typical mark allocations range from 1-mark definitions of keywords such as ‘encapsulation’ to 6-mark questions comparing inheritance with composition.
典型分值从关键词定义(如”封装”)的 1 分题,到比较继承与组合的 6 分题不等。
You should be confident writing basic class skeletons, identifying errors in given code, and explaining how OOP principles improve maintainability.
你应该能熟练编写基本类框架、识别给定代码中的错误,并解释 OOP 原则如何提高可维护性。
2. From Procedural to Object-Oriented | 从过程式到面向对象
Procedural programming organises code as a sequence of instructions and reusable functions, while OOP bundles data and behaviour together into classes.
过程式编程将代码组织为一系列指令和可复用函数,而面向对象编程将数据和行为捆绑到类中。
The key difference is that an object has state (attribute values) and behaviour (methods) that act on that state, allowing more natural modelling of real-world systems.
关键区别在于对象具有状态(属性值)和行为(作用于状态的方法),从而更自然地建模现实世界系统。
For example, a BankAccount object can have a balance attribute and deposit() and withdraw() methods, rather than passing a balance variable to separate functions.
例如,一个 BankAccount 对象可以拥有 balance 属性和 deposit()、withdraw() 方法,而不是将 balance 变量传递给独立函数。
3. Classes and Objects | 类与对象
A class is a blueprint or template that defines the attributes and methods common to all objects of that type.
类是一份蓝图或模板,定义了该类型所有对象共有的属性和方法。
An object is an instance of a class, created at runtime with its own copies of attribute values.
对象是类的一个实例,在运行时创建,拥有自己的属性值副本。
In Python, class Dog: defines the class, while d = Dog() creates an instance called d.
在 Python 中,class Dog: 定义类,而 d = Dog() 创建一个名为 d 的实例。
In Java, the equivalent is public class Dog { } and Dog d = new Dog();.
在 Java 中,等价写法是 public class Dog { } 和 Dog d = new Dog();。
4. Attributes and Methods | 属性与方法
Attributes store the state of an object and are typically declared inside the constructor or at the top of the class.
属性存储对象的状态,通常在构造函数内或类顶部声明。
Methods define the behaviour of an object and usually access or modify attributes through a self or this reference.
方法定义对象的行为,通常通过 self 或 this 引用来访问或修改属性。
In Python, you write def bark(self): print(‘Woof’), while in Java you write public void bark() { System.out.println(“Woof”); }.
在 Python 中,写 def bark(self): print(‘Woof’),而在 Java 中写 public void bark() { System.out.println(“Woof”); }。
There are also class attributes (static fields) shared by all instances, but instance attributes are the most common exam focus.
还有由所有实例共享的类属性(静态字段),但实例属性是最常见的考试重点。
5. Encapsulation and Access Modifiers | 封装与访问修饰符
Encapsulation means keeping an object’s internal data private and providing controlled access through public methods, often called getters and setters.
封装意味着将对象的内部数据保持私有,并通过公共方法(通常称为 getter 和 setter)提供受控访问。
This protects the integrity of the data because validation can be placed inside the setter, preventing impossible states such as a negative age.
这保护了数据的完整性,因为可以在 setter 中加入验证,防止出现负年龄等不可能的状态。
Java uses private, public and protected keywords, while Python conventionally uses a single underscore prefix (_balance) to signal ‘protected’ but does not enforce it strictly.
Java 使用 private、public 和 protected 关键字,而 Python 传统上使用单下划线前缀(_balance)来表示”受保护”,但并不严格强制。
In exams, you may be asked to explain why directly exposing attributes is considered poor practice and how encapsulation supports validation and maintenance.
考试中可能会要求你解释为什么直接暴露属性是不良实践,以及封装如何支持验证和维护。
6. Constructors and Instantiation | 构造函数与实例化
A constructor is a special method that initialises a new object, setting the initial values of attributes.
构造函数是一种特殊方法,用于初始化新对象,设置属性的初始值。
In Python, the constructor is named __init__ and takes self as the first parameter, e.g. def __init__(self, name, age): self.name = name.
在 Python 中,构造函数名为 __init__,第一个参数是 self,例如 def __init__(self, name, age): self.name = name。
In Java, the constructor has the same name as the class and no return type: public Dog(String name) { this.name = name; }.
在 Java 中,构造函数与类同名且没有返回类型:public Dog(String name) { this.name = name; }。
Default constructors are provided automatically if no constructor is written, but once you define a parameterised constructor the default disappears unless you write it again.
如果没有编写构造函数,会自动提供默认构造函数,但一旦定义了带参数的构造函数,默认构造函数就会消失,除非重新编写。
7. Inheritance and Subclasses | 继承与子类
Inheritance allows a new class (subclass) to reuse, extend or override the attributes and methods of an existing class (superclass).
继承允许新类(子类)复用、扩展或重写现有类(超类)的属性和方法。
You indicate inheritance in Python with class Child(Parent): and in Java with class Child extends Parent { }.
在 Python 中用 class Child(Parent): 表示继承,在 Java 中用 class Child extends Parent { }。
A classic exam example is a superclass Vehicle with subclasses Car and Motorcycle that inherit the start() method but add their own characteristics.
经典的考试示例是超类 Vehicle 和子类 Car、Motorcycle,子类继承 start() 方法但添加自己的特征。
Constructors of subclasses must call the superclass constructor, using super().__init__(…) in Python or super(…) in Java, to ensure inherited attributes are set up.
子类的构造函数必须调用超类构造函数,使用 Python 的 super().__init__(…) 或 Java 的 super(…),以确保继承的属性被正确设置。
8. Polymorphism and Overriding | 多态与方法重写
Polymorphism means ‘many forms’ and allows the same method call to behave differently depending on the object’s actual class.
多态意为”多种形态”,允许相同的方法调用根据对象的实际类表现出不同的行为。
Method overriding occurs when a subclass defines a method with the same signature as a superclass method, replacing its implementation for subclass objects.
方法重写发生在子类定义与超类方法签名相同的方法时,为子类对象替换该方法的实现。
For example, a list of Shape objects may contain Circle and Square instances, and calling shape.area() dispatches to the correct override at runtime.
例如,一个 Shape 对象列表可能包含 Circle 和 Square 实例,调用 shape.area() 在运行时分派到正确的重写方法。
In Python, polymorphism is achieved dynamically by simply defining a method with the same name; in Java, use @Override annotation for clarity.
在 Python 中,多态通过动态地定义同名方法实现;在 Java
Published by TutorHao | A-Level 编程 Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导