📚 Object-Oriented Programming for Edexcel A-Level Computer Science | Edexcel A-Level 计算机:面向对象编程核心概念与考试应用
Object-oriented programming (OOP) is a central topic in the Edexcel A-Level Computer Science specification. It is the basis for designing reusable, maintainable and secure code, and it underpins the way large software systems are planned, built and tested. This revision guide covers the key OOP concepts you need for both Paper 1 and Paper 2 style questions, including classes, objects, encapsulation, inheritance, polymorphism and object relationships. Every section pairs the theory with the kind of wording examiners expect to see in a full-mark answer.
面向对象编程(OOP)是 Edexcel A-Level 计算机科学大纲中的核心主题。它是设计可重用、可维护且安全代码的基础,也是大型软件系统规划、构建与测试的底层思想。本复习指南涵盖 Paper 1 和 Paper 2 中可能出现的 OOP 关键概念,包括类、对象、封装、继承、多态和对象关系。每一节都将理论讲解与考官期待的高分表述方式配对呈现,帮助你写出踩点准确的答案。
1. Programming Paradigms: Procedural vs Object-Oriented | 编程范式:过程式与面向对象
A programming paradigm is a fundamental style or way of programming. Procedural programming structures code as a sequence of instructions and functions that operate on data. Object-oriented programming instead organises code around objects, which combine data and the methods that act on that data. In a procedural program, data is often held in global variables that any function can change, so a single unexpected change can break unrelated parts of the system. In an object-oriented program, data is kept inside the object that owns it, and other parts of the program can only interact with it through a defined set of methods.
编程范式是一种基本的编程风格或方式。过程式编程将代码结构化为一系列操作数据的指令和函数。而面向对象编程围绕对象来组织代码,对象将数据及操作这些数据的方法组合在一起。在过程式程序中,数据常存放在全局变量里,任何函数都能修改,因此一次意外的改动可能破坏系统中互不相关的部分。而在面向对象程序中,数据保存在拥有它的对象内部,程序的其他部分只能通过一组已定义好的方法与其交互。
- Procedural: separates data and functions; uses top-down design; examples include C, Pascal and older BASIC. | 过程式:将数据与函数分离;使用自顶向下设计;例如 C、Pascal 和早期 BASIC。
- Object-oriented: bundles data and methods; supports abstraction, encapsulation and reuse; examples include Java, C++ and Python. | 面向对象:将数据和方法捆绑;支持抽象、封装和重用;例如 Java、C++ 和 Python。
| Aspect | 比较维度 | Procedural | 过程式 | Object-oriented | 面向对象 |
| Basic unit | 基本单位 | Function / procedure | 函数 / 过程 | Class and object | 类与对象 |
| Data location | 数据位置 | Separate, often global | 分离,常为全局 | Encapsulated inside objects | 封装在对象内部 |
| Reuse mechanism | 复用方式 | Libraries of functions | 函数库 | Inheritance and composition | 继承与组合 |
| Large-system maintenance | 大型系统维护 | Harder as the program grows | 程序变大后更困难 | Easier, modules can be changed in isolation | 更易,模块可独立修改 |
| Typical languages | 典型语言 | C, Pascal, BASIC | C、Pascal、BASIC | Java, C++, Python, C# | Java、C++、Python、C# |
For a full-mark comparison answer you should name the four pillars of OOP: abstraction, encapsulation, inheritance and polymorphism. Examiners also like a practical consequence, for example that encapsulation reduces the risk of accidental data corruption while inheritance reduces duplicated code.
要拿到比较类题的满分,你应当点出 OOP 的四大支柱:抽象、封装、继承和多态。考官也喜欢看到实际后果,例如封装降低了数据被意外破坏的风险,而继承减少了重复代码。
2. Classes and Objects | 类与对象
A class is a template or blueprint that defines the attributes and methods common to all objects of a certain kind. It does not store actual data itself; rather, it describes what data and behaviour its objects will have. An object is a specific instance of a class created at run time, and it does hold its own values for every attribute. The act of creating an object from a class is called instantiation, and in most languages it uses the keyword new followed by a call to the constructor.
类是定义某一类对象共有属性和方法的模板或蓝图。它本身并不存储实际数据,而是描述其对象将拥有哪些数据和行为。对象则是运行时创建的类的具体实例,它为每一个属性持有自己的取值。由类创建对象的过程称为实例化,在多数语言中使用关键字 new 并调用构造方法完成。
Consider a class called Student. The class defines that every student has a name, a year group and a mark. Three separate Student objects might hold ('Amir', 12, 78), ('Beth', 12, 64) and ('Chloe', 13, 91). Each object has the same structure but different state. This distinction between the shared definition and the individual instance is one of the most frequently examined ideas in the specification.
设想一个名为 Student 的类。该类定义每个学生都有姓名、年级和成绩。三个不同的 Student 对象可能分别保存('Amir', 12, 78)、('Beth', 12, 64)和('Chloe', 13, 91)。每个对象结构相同,但状态不同。这种"共享定义"与"独立实例"的区分,是大纲中最常考查的思想之一。
- Class: compile-time definition; describes attributes and methods. | 类:编译期的定义;描述属性与方法。
- Object: run-time instance; holds its own values for the attributes. | 对象:运行期的实例;为各属性保存自己的取值。
- Instantiation: creating an object using new and a constructor. | 实例化:使用 new 和构造方法创建对象。
3. Attributes, Methods and Constructors | 属性、方法与构造方法
An attribute, also called a field or instance variable, stores part of an object's state. Each object gets its own copy of every instance attribute, so changing one object's name does not affect any other object. A method is a subroutine that defines the behaviour of the class; it can read attributes, modify them, or return a computed value. In the exam you may be asked to distinguish between the state of an object (its attribute values) and its behaviour (its methods).
属性又称字段或实例变量,用于保存对象状态的一部分。每个对象都拥有所有实例属性各自的副本,因此修改一个对象的姓名不会影响其他对象。方法是定义类行为的子程序,它可以读取属性、修改属性或返回计算结果。考试中可能要求你区分对象的状态(属性取值)与行为(方法)。
A constructor is a special method that runs automatically when an object is created. It normally has the same name as the class, has no return type, and its job is to initialise the attributes so that the object starts life in a valid state. Constructors can be overloaded, which means a class may offer several constructors with different parameter lists.
构造方法是一种特殊方法,在创建对象时自动运行。它通常与类同名、没有返回类型,其职责是初始化属性,使对象一诞生就处于合法状态。构造方法可以重载,也就是一个类可以提供多个参数列表不同的构造方法。
CLASS Student PRIVATE name : STRING PRIVATE yearGroup : INTEGER PRIVATE mark : INTEGER PUBLIC PROCEDURE Student(newName, newYear) name ← newName yearGroup ← newYear mark ← 0 END PROCEDURE PUBLIC FUNCTION getMark() RETURN mark END FUNCTION END CLASS
Note the two extra ideas examiners sometimes reward. A class variable (marked static in Java) is shared by every object of the class rather than copied, and a class method can be called without creating an object at all. Use these only when the question specifically asks about shared data.
还有两个附加概念常被考官加分。类变量(在 Java 中标记为 static)由该类的所有对象共享而非各自复制;类方法无需创建对象即可调用。只有在题目明确询问共享数据时才使用这些概念。
4. Encapsulation and Information Hiding | 封装与信息隐藏
Encapsulation is the bundling of data and the methods that operate on that data into a single unit, the class, together with the restriction of direct access to the internal data. Information hiding is the related goal of exposing only the interface that other parts of the program genuinely need. Encapsulation is the mechanism; information hiding is the effect. Together they mean that an object's attributes are private and can only be changed through its own public methods.
封装是把数据与操作这些数据的方法捆绑成单一单元(类),并限制对内部数据的直接访问。信息隐藏则是与之相关的目标:只暴露程序其他部分真正需要的接口。封装是机制,信息隐藏是效果。二者结合意味着对象的属性是私有的,只能通过其自身的公有方法进行修改。
- Prevents invalid states: a setter can reject a negative age or a mark above 100. | 防止非法状态:设置器可以拒绝负数年龄或超过 100 的成绩。
- Easier maintenance: the internal implementation can be rewritten without breaking code that uses the class. | 更易维护:内部实现可以重写,而不会破坏使用该类的代码。
- Reduced coupling: other classes depend on a small, stable interface rather than on raw data. | 降低耦合:其他类依赖小而稳定的接口,而非原始数据。
- Security: sensitive values cannot be changed by any part of the program that happens to know the variable name. | 安全性:敏感数值不会被任何碰巧知道变量名的代码修改。
A useful analogy for a written answer is a cash machine. You can deposit and withdraw money through the keypad, but you cannot reach inside and edit the stored balance directly. The keypad is the public interface and the vault is the encapsulated data.
写答案时可用的类比是自动取款机:你可以通过键盘存款和取款,但无法伸手进去直接修改存储的余额。键盘就是公有接口,金库就是被封装的私有数据。
5. Access Modifiers, Getters and Setters | 访问修饰符、获取器与设置器
Access modifiers control the visibility of attributes and methods. The three you must know are public, private and protected. A public member is visible to all classes, a private member is visible only inside its own class, and a protected member is visible inside its own class and to subclasses.
访问修饰符控制属性与方法的可见性。你必须掌握的三个是 public、private 和 protected。公有成员对所有类可见;私有成员仅在其自身类内可见;受保护成员在其自身类及其子类内可见。
| Modifier | 修饰符 | Visible to | 可见范围 | Typical use | 典型用途 |
| public | 公有 | Every class | 所有类 | Methods that form the interface | 构成接口的方法 |
| private | 私有 | The defining class only | 仅定义它的类 | All instance attributes | 所有实例属性 |
| protected | 受保护 | The class and its subclasses | 本类及其子类 | Data shared down a hierarchy | 沿继承层次共享的数据 |
Because attributes are normally private, each class provides accessor methods, usually called getters, and mutator methods, usually called setters. A getter returns the current value of an attribute and a setter replaces it, typically after validation. This is where encapsulation becomes visible in code, and it is a very common exam task: write a setter that only accepts a mark between 0 and 100.
由于属性通常是私有的,每个类都会提供访问器方法(通常称为获取器)和修改器方法(通常称为设置器)。获取器返回属性的当前值,设置器替换该值,通常先做验证。封装正是在这里体现在代码中,这也是极常见的考题:编写一个只接受 0 到 100 之间成绩的设置器。
PUBLIC PROCEDURE setMark(newMark) IF newMark >= 0 AND newMark <= 100 THEN mark ← newMark ELSE OUTPUT "Invalid mark" END IF END PROCEDURE
When asked to explain why attributes are private but methods are public, structure the answer as: private attributes prevent external code changing data without validation, while public methods provide a controlled and documented way to read and update that data.
当被要求解释为什么属性私有而方法公有时,可按此结构作答:私有属性防止外部代码不经校验地改动数据,而公有方法提供了受控且已文档化的读取与更新途径。
6. Inheritance | 继承
Inheritance allows a new class, called the subclass or derived class, to acquire the attributes and methods of an existing class, called the superclass or base class. It models an is-a relationship: a Car is a Vehicle, a Circle is a Shape, a SavingsAccount is a BankAccount. The subclass inherits all non-private members of the superclass, may add new attributes and methods of its own, and may override inherited methods to change their behaviour.
继承允许一个新类(称为子类或派生类)获取已有类(称为父类或基类)的属性和方法。它建模的是一种"is-a"(是一个)关系:汽车是一种交通工具,圆是一种形状,储蓄账户是一种银行账户。子类继承父类全部非私有成员,可以增加自己的新属性和方法,也可以重写继承来的方法以改变其行为。
In Java, a subclass is declared using the keyword extends and calls its parent's constructor with super(...) as the first statement in its own constructor. This guarantees that the inherited part of the object is fully initialised before the subclass adds its own state. Private attributes of the superclass are still inherited, but they must be reached through public or protected accessor methods.
在 Java 中,子类使用关键字 extends 声明,并在自身构造方法的第一条语句用 super(...) 调用父类构造方法。这确保对象的继承部分在子类添加自身状态之前被完整初始化。父类的私有属性仍然被继承,但必须通过公有或受保护的访问器方法来访问。
- Benefits: code reuse, a logical hierarchy, easier extension, and it enables polymorphism. | 优点:代码复用、逻辑层次清晰、易于扩展,并且使多态成为可能。
- Drawbacks: tight coupling between parent and child, so a change to the superclass can break subclasses; hierarchies can become deep and hard to follow. | 缺点:父子类之间高度耦合,父类的改动可能破坏子类;继承层次过深会难以理解。
- Types: single inheritance (one parent) is standard in Java; multiple inheritance of classes is not allowed in Java, but a class may implement several interfaces. | 类型:单继承(一个父类)在 Java 中是标准做法;Java 不允许多重类继承,但一个类可以实现多个接口。
A typical exam diagram shows Shape at the top, with Circle, Rectangle and Triangle beneath it. Circle inherits area() and perimeter() from Shape but overrides both with its own formulas.
典型的考试图示是顶端为 Shape,下方为 Circle、Rectangle 和 Triangle。Circle 从 Shape 继承 area() 和 perimeter(),但用自身公式重写这两个方法。
7. Polymorphism | 多态
Polymorphism means 'many forms', and it describes the ability of the same method call to behave differently depending on the object that receives it. Edexcel expects you to distinguish the two main kinds. Method overloading is compile-time polymorphism: several methods share a name but have different parameter lists, and the compiler chooses the right one from the arguments supplied. Method overriding is run-time polymorphism: a subclass replaces an inherited method with the same signature, and the version that runs is decided when the program executes, based on the actual type of the object.
多态意为"多种形态",它描述同一次方法调用会因接收该调用的对象不同而产生不同行为的能力。Edexcel 要求你区分两种主要形式。方法重载是编译期多态:多个方法同名但参数列表不同,编译器根据传入实参选择正确的版本。方法重写是运行期多态:子类以相同签名替换继承来的方法,实际运行哪个版本在程序执行时依据对象的真实类型决定。
Shape s s ← new Circle(3.0) OUTPUT s.area() // runs Circle's version at run time s ← new Rectangle(4.0, 5.0) OUTPUT s.area() // runs Rectangle's version
The variable s has the type Shape, but the object it refers to determines which area() method is executed. This mechanism is called dynamic dispatch or late binding. Polymorphism matters because it lets you write one loop over a collection of shapes without a long chain of if statements testing each type, which makes the program shorter, easier to extend and easier to test.
变量 s 的类型是 Shape,但它所引用的对象决定了执行哪个 area() 方法。这一机制称为动态绑定或后期绑定。多态之所以重要,是因为它让你能对一个形状集合写一个循环,而不用写一长串 if 语句逐个判断类型,从而使程序更短、更易扩展、更易测试。
| Feature | 特征 | Overloading | 重载 | Overriding | 重写 |
| Where | 位置 | Same class | 同一个类内 | Superclass and subclass | 父类与子类之间 |
| Signature | 签名 | Different parameter list | 参数列表不同 | Identical signature | 签名完全相同 |
| Resolved | 决定时机 | Compile time | 编译期 | Run time | 运行期 |
| Also called | 又称 | Static polymorphism | 静态多态 | Dynamic polymorphism | 动态多态 |
8. Abstraction, Abstract Classes and Interfaces | 抽象、抽象类与接口
Abstraction means hiding the complex detail of how something works and exposing only the essential features needed to use it. When you call a method such as sort(), you do not need to know which sorting algorithm is used. Abstraction reduces complexity, supports decomposition, and lets the designer model the
更多咨询请联系16621398022(同微信)

或截图后用「扫一扫 → 相册」选择本图