📚 Object-Oriented Programming Essentials for Edexcel A-Level Computer Science | Edexcel A-Level 计算机科学面向对象编程核心要点
Object-oriented programming (OOP) is a core part of the Edexcel A-Level Computer Science specification. This article explains the key concepts examiners expect you to use accurately: classes, objects, inheritance, encapsulation, polymorphism and design thinking.
面向对象编程(OOP)是 Edexcel A-Level 计算机科学课程的核心内容。本文解释了考试要求你准确使用的关键概念:类、对象、继承、封装、多态和设计思维。
1. Programming Paradigms and the Need for OOP | 编程范式与面向对象编程的必要性
Edexcel expects you to contrast procedural programming with object-oriented programming. A procedural program organises code into functions that operate on separate data, whereas OOP bundles data and the methods that act on it into objects.
Edexcel 考试要求你对比面向过程编程与面向对象编程。面向过程程序将代码组织为操作独立数据的函数,而面向对象编程则将数据与操作这些数据的方法捆绑到对象中。
OOP is useful for large systems because it promotes modularity, code reuse and easier maintenance. Real-world entities such as customers, accounts and orders can be modelled naturally as objects.
面向对象编程对大型系统非常有用,因为它能促进模块化、代码复用和易于维护。客户、账户和订单等现实世界实体都可以被自然地建模为对象。
2. Classes and Objects: Defining Blueprints | 类与对象:定义蓝图
A class is a template or blueprint that defines the attributes and methods shared by a group of objects. An object is a specific instance of a class, created at runtime.
类是定义一组对象共享的属性和方法的模板或蓝图。对象是类的具体实例,在运行时创建。
For example, a Student class may define attributes such as name and yearGroup, and methods such as enrol(). Each individual student would then be an object of that class.
例如,Student 类可以定义 name 和 yearGroup 等属性,以及 enrol() 等方法。每个具体的学生就是该类的一个对象。
In exam answers, you must distinguish clearly between the class and the object. Saying ‘an object is a blueprint’ is a common mark-losing error; the class is the blueprint, the object is the instance.
在考试答案中,你必须清楚区分类和对象。“对象是蓝图”是一种常见的失分错误;类才是蓝图,对象是实例。
3. Attributes, Methods and Constructors | 属性、方法与构造函数
Attributes store the state of an object, while methods define its behaviour. A constructor is a special method used to initialise a new object’s attributes when it is instantiated.
属性存储对象的状态,而方法定义对象的行为。构造函数是一种特殊方法,用于在对象实例化时初始化新对象的属性。
In Python-style pseudocode, a constructor is written as __init__(self). The self parameter refers to the current object being created or used.
在 Python 风格伪代码中,构造函数写作 __init__(self)。self 参数引用当前正在创建或使用的对象。
Consider a BankAccount class with a constructor that sets the account number and opening balance. This ensures every new account object starts in a valid state.
考虑一个 BankAccount 类,其构造函数设置账号和初始余额。这样可确保每个新账户对象都从一个有效状态开始。
4. Encapsulation and Access Modifiers | 封装与访问修饰符
Encapsulation means bundling attributes and methods inside a class and controlling access to the internal state. It protects data from accidental or unauthorised change.
封装意味着将属性和方法捆绑在类内部,并控制对内部状态的访问。它保护数据免受意外或未经授权的更改。
Access modifiers such as private, public and protected determine visibility. Exam questions often ask why attributes should be private and accessed through public getter and setter methods.
private、public 和 protected 等访问修饰符决定了可见性。考试题经常问为什么属性应为私有,并通过公共的 getter 和 setter 方法访问。
| Modifier | Meaning | 中文含义 |
private |
Only accessible within the same class | 仅在同一类内可访问 |
public |
Accessible from any class | 任何类均可访问 |
protected |
Accessible within the class and its subclasses | 类及其子类可访问 |
Encapsulation also improves maintainability because the internal implementation can change without affecting code that uses the public interface.
封装还能提高可维护性,因为内部实现可以改变,而不会影响使用公共接口的代码。
5. Inheritance: Building Class Hierarchies | 继承:构建类层次结构
Inheritance allows a new class, called a subclass, to reuse and extend the attributes and methods of an existing class, called a superclass. It represents an ‘is-a’ relationship.
继承允许新类(称为子类)复用并扩展现有类(称为父类)的属性和方法。它表示一种“is-a”关系。
For example, a Dog class can inherit from an Animal class. The Dog class automatically has the features of Animal, but can also add a specific bark() method.
例如,Dog 类可以继承自 Animal 类。Dog 类自动具有 Animal 的特征,也可以添加特定的 bark() 方法。
Method overriding occurs when a subclass provides its own version of a method defined in the superclass. This is essential for customising behaviour without changing the superclass code.
当子类提供父类中已定义方法的自身版本时,就发生了方法重写。这对于在不更改父类代码的情况下自定义行为至关重要。
6. Polymorphism: Overriding and Overloading | 多态:重写与重载
Polymorphism means ‘many forms’. It allows objects of different classes to respond to the same method call in different ways, provided they share a common interface or superclass.
多态意为“多种形态”。它允许不同类的对象对同一方法调用作出不同响应,前提是它们共享一个公共接口或父类。
Method overriding is the key form of polymorphism tested at A-Level. A parent reference can point to a child object, and the appropriate overridden method is selected at runtime.
方法重写是 A-Level 考试中测试的多态的主要形式。父类引用可以指向子类对象,运行时将选择适当的被重写方法。
Method overloading is having multiple methods with the same name but different parameter lists. Edexcel may mention it in Java-style contexts, but you should not confuse overloading with overriding.
方法重载是指具有多个同名但参数列表不同的方法。Edexcel 可能在 Java 风格上下文中提到它,但你不应将重载与重写混淆。
7. Aggregation and Composition | 聚合与组合
Aggregation and composition describe ‘has-a’ relationships between objects. They are both forms of association, but they differ in object lifetime dependency.
聚合和组合描述对象之间的“has-a”关系。它们都是关联的形式,但对象生命周期依赖性不同。
In aggregation, the contained object can exist independently of the container. A Library object may contain Book objects, but a book can still exist if the library is deleted.
在聚合中,被包含对象可以独立于容器存在。Library 对象可以包含 Book 对象,但如果图书馆被删除,书仍然可以存在。
In composition, the contained object cannot exist without the container. For example, a Car has an Engine, and if the car is destroyed, the engine object is also destroyed.
在组合中,被包含对象不能脱离容器存在。例如,Car 有一个 Engine,如果汽车被销毁,引擎对象也会被销毁。
8. Abstract Classes and Interfaces | 抽象类与接口
An abstract class is a class that cannot be instantiated directly. It may contain abstract methods, which have no implementation and must be overridden by concrete subclasses.
抽象类是不能直接实例化的类。它可以包含抽象方法,抽象方法没有实现,必须由具体子类重写。
An interface defines a contract of method signatures that implementing classes must provide. Unlike abstract classes, a class can implement multiple interfaces, which supports a form of multiple inheritance.
接口定义了实现类必须提供的方法签名契约。与抽象类不同,一个类可以实现多个接口,从而支持一种多重继承形式。
Use abstract classes when classes share common state or code, and use interfaces when unrelated classes must provide the same behaviour.
当类共享公共状态或代码时使用抽象类,当不相关的类必须提供相同行为时使用接口。
9. OOP Design Principles and Exam Technique | 面向对象设计原则与考试技巧
Exam questions often give a scenario and ask you to identify suitable classes, attributes and methods. Start by listing the nouns, which often become classes, and verbs, which often become methods.
考试题常给出一个场景,要求你识别合适的类、属性和方法。首先列出名词,名词通常成为类;列出动词,动词通常成为方法。
Use precise terminology in your answers. For example, say ‘the subclass inherits from the superclass’ rather than ‘the child takes the parent’s stuff’.
答案中要使用准确术语。例如,说“子类继承自父类”,而不是“子类拿了父类的东西”。
Design principles such as low coupling and high cohesion are valued. Classes should have one clear responsibility and should not depend unnecessarily on other classes.
低耦合和高内聚等设计原则受到重视。类应该只有一个明确职责,并且不应不必要地依赖其他类。
10. Common Pitfalls and Mark Scheme Language | 常见误区与评分标准用语
One common mistake is saying that encapsulation exists only to hide data. It also provides a controlled interface and improves maintainability, so explain both ideas in longer questions.
一个常见误区是说封装只是为了隐藏数据。封装还提供受控接口并提高可维护性,因此在较长问题中要解释这两个方面。
Another pitfall is confusing inheritance with aggregation. Inheritance models ‘is-a’, while aggregation and composition model ‘has-a’. Writing ‘a car is an engine’ would lose marks.
另一个误区是混淆继承与聚合。继承表示“is-a”,而聚合和组合表示“has-a”。写出“汽车是一个引擎”会失分。
Examiners look for phrases such as ‘instantiate an object’, ‘call the constructor’, ‘override the method’ and ‘encapsulate the attributes’. Practise using these terms under timed conditions.
考官期待看到“实例化对象”“调用构造函数”“重写方法”“封装属性”等用语。请在限时条件下练习使用这些术语。
Published by TutorHao | Programming Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导Cancel reply