📚 Object-Oriented Programming for Edexcel A-Level Computer Science | Edexcel A-Level 计算机科学中的面向对象编程
Object-oriented programming (OOP) is a key programming paradigm assessed in Edexcel A-Level Computer Science. It models real-world systems using classes and objects, focusing on data and the operations that act on that data. A clear understanding of terminology such as encapsulation, inheritance and polymorphism is essential for both Paper 1 and Paper 2.
面向对象编程(OOP)是 Edexcel A-Level 计算机科学重点考查的编程范式之一。它使用类和对象对现实世界建模,强调数据以及作用于数据的操作。清楚理解封装、继承和多态等术语对于 Paper 1 和 Paper 2 都至关重要。
1. Programming Paradigms | 编程范式
A paradigm is a style or way of programming. The main paradigms are procedural, object-oriented, event-driven and functional. In the Edexcel specification, you need to compare OOP with procedural programming, especially how OOP groups data and methods into classes.
范式是指编程的风格或方式。主要范式包括面向过程、面向对象、事件驱动和函数式。在 Edexcel 考纲中,需要比较 OOP 与面向过程编程,尤其要理解 OOP 如何将数据和方法组合到类中。
2. Classes and Objects | 类与对象
A class is a blueprint or template that defines the attributes and methods shared by a group of objects. An object is an instance of a class. For example, the class Car defines the data and behaviour of cars; the objects car1 and car2 each hold their own values for registration, make and mileage.
类是定义一组对象共有的属性和方法的蓝图或模板。对象是类的实例。例如,类 Car 定义了汽车的数据和行为;对象 car1 和 car2 分别保存自己的车牌、品牌和里程值。
3. Attributes and Methods | 属性与方法
Attributes represent the state or data of an object, such as colour, speed and fuel level. Methods represent the behaviour or operations, such as accelerate(), brake() and getSpeed(). In a class diagram, attributes are usually listed in the top compartment and methods in the bottom compartment.
属性表示对象的状态或数据,例如颜色、速度和油量。方法表示行为或操作,如 accelerate()、brake() 和 getSpeed()。在类图中,属性通常列在上方,方法列在下方。
4. Encapsulation | 封装
Encapsulation means hiding the internal state of an object and forcing access through public methods. Attributes are usually declared private, and public getter and setter methods allow controlled read and write access. This protects data integrity and makes the code easier to maintain.
封装是指隐藏对象的内部状态,并通过公有方法强制访问。属性通常声明为私有,公有 getter 和 setter 方法提供受控的读写访问。这样可以保护数据完整性,并使代码更易维护。
private int fuel; public int getFuel() { return fuel; }
5. Constructors and Instantiation | 构造函数与实例化
Instantiation is the process of creating an object from a class using the keyword new in Java-like languages or a constructor call in Python. A constructor is a special method that runs automatically when a new object is created and usually initialises the attributes.
实例化是从类创建对象的过程,在类似 Java 的语言中使用关键字 new,或在 Python 中通过构造函数调用完成。构造函数是一种特殊方法,在创建新对象时自动执行,通常用于初始化属性。
Car c1 = new Car("AB12 CDE", 0);
6. Inheritance | 继承
Inheritance allows a subclass to acquire the attributes and methods of a superclass. The subclass can add new members or override inherited methods. For example, ElectricCar inherits from Car and adds the attribute batteryCapacity and the method charge().
继承允许子类获得父类的属性和方法。子类可以添加新成员,或重写继承的方法。例如,ElectricCar 继承 Car,并添加属性 batteryCapacity 和方法 charge()。
7. Polymorphism | 多态
Polymorphism means 'many forms'. It allows a method call to behave differently depending on the object type. In practice, a superclass method such as drive() can be overridden in subclasses, and a collection of Car objects can contain both Car and ElectricCar instances.
多态的意思是“多种形态”。它允许同一个方法调用根据对象类型表现不同。实际中,父类方法如 drive() 可以在子类中被重写,而一个 Car 对象集合可以同时包含 Car 和 ElectricCar 实例。
- Compile-time polymorphism: method overloading | 编译时多态:方法重载
- Runtime polymorphism: method overriding | 运行时多态:方法重写
8. Association, Aggregation and Composition | 关联、聚合与组合
These describe relationships between classes. Association is a general 'has-a' relationship. Aggregation is a weaker whole-part relationship where parts can exist independently. Composition is a stronger whole-part relationship where parts cannot exist without the whole.
这些概念描述类之间的关系。关联是通用的“拥有”关系。聚合是较弱的整体-部分关系,部分可以独立存在。组合是较强的整体-部分关系,部分不能脱离整体存在。
| Relationship | 关系 | Symbol | 符号 | Example | 示例 |
|---|---|---|
| Association | 关联 | — | Library — Book | 图书馆 — 图书 |
| Aggregation | 聚合 | ◇— | Team ◇— Player | 球队 ◇— 球员 |
| Composition | 组合 | ◆— | House ◆— Room | 房子 ◆— 房间 |
9. UML Class Diagrams | UML 类图
UML class diagrams show classes as rectangles divided into three parts: name, attributes and methods. Visibility markers include + for public, - for private and # for protected. Inheritance is shown with a hollow triangle arrow pointing to the superclass.
UML 类图将类表示为分为三个部分的矩形:名称、属性和方法。可见性标记包括 + 表示公有、- 表示私有、# 表示受保护。继承用指向父类的空心三角箭头表示。
10. Advantages and Disadvantages | 优缺点
OOP improves reuse, maintainability and modularity, and it models real-world problems naturally. However, it can introduce complexity, require more memory and produce slower performance for small programs than procedural code.
OOP 提高了可重用性、可维护性和模块化程度,并且能自然地模拟现实问题。但对于小程序,它可能引入复杂性、占用更多内存,运行速度也可能低于面向过程代码。
| Advantages | 优点 | Disadvantages | 缺点 |
|---|---|
| Reuse through inheritance | 通过继承复用 | Steeper learning curve | 学习曲线较陡 |
| Easier maintenance and modularity | 更易维护和模块化 | More memory and overhead | 更多内存和开销 |
| Natural modelling of real systems | 自然模拟现实系统 | Less efficient for tiny tasks | 对小任务效率较低 |
11. Exam Tips and Common Errors | 考试提示与常见错误
In Edexcel exams, define OOP terms precisely and support them with a clear example. A common error is confusing an object with a class, or stating that inheritance is copying code instead of reusing behaviour through a class hierarchy. Show relationships accurately in UML diagrams, and use correct visibility markers.
在 Edexcel 考试中,要准确定义 OOP 术语,并用清晰示例支撑。常见错误包括混淆对象与类,或将继承说成复制代码,而不是通过类层次结构复用行为。在 UML 图中准确表示关系,并使用正确的可见性标记。
- Class vs object confusion | 混淆类与对象
- Incorrect visibility symbols | 可见性符号错误
- Confusing aggregation and composition | 混淆聚合与组合
Published by TutorHao | Computer Science Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)

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