📚 Object-Oriented Programming for Edexcel A-Level | Edexcel A-Level 面向对象编程精讲
Object-oriented programming (OOP) is a central topic in the Edexcel A-Level Computer Science specification. It shifts the focus from a sequence of instructions to modelling real-world entities as objects that contain both data and behaviour. Understanding OOP is essential for Paper 2 programming questions and for designing maintainable software systems.
面向对象编程(OOP)是爱德思 A-Level 计算机科学考试大纲中的核心主题。它将编程的关注点从指令序列转移到把现实世界的实体建模为同时包含数据与行为的对象。理解 OOP 对于 Paper 2 编程题以及设计可维护的软件系统至关重要。
1. Programming Paradigms | 编程范式
A programming paradigm is a fundamental style of programming. The main paradigms examined by Edexcel are procedural, object-oriented, and event-driven programming. Each paradigm organises code differently and suits different types of problem.
编程范式是一种基本的编程风格。爱德思考试主要考查过程式、面向对象和事件驱动三种编程范式。每种范式以不同的方式组织代码,适合不同类型的问题。
Procedural programming breaks a problem into procedures or functions that manipulate shared data. It is straightforward but can become hard to maintain as a project grows because data and functions are separate.
过程式编程将问题分解为操作共享数据的过程或函数。它直观简单,但随着项目规模扩大,由于数据与函数彼此分离,维护会变得困难。
Object-oriented programming bundles data and the methods that operate on that data into classes. This encapsulation makes large systems easier to model, extend, and debug.
面向对象编程将数据以及操作这些数据的方法捆绑到类中。这种封装使得大型系统更易于建模、扩展和调试。
Event-driven programming responds to user actions such as clicks and key presses. It is often used in graphical user interfaces, where the flow of execution is determined by events rather than a fixed sequence.
事件驱动编程响应用户操作,例如点击和按键。它常用于图形用户界面,其执行流程由事件决定,而不是固定的顺序。
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 a specific instance of a class, created from that blueprint with its own state.
类是定义一组对象共同属性和方法的蓝图或模板。对象是类的具体实例,根据该蓝图创建并拥有自己的状态。
For example, a class Car might have attributes such as colour, engineSize, and registration, and methods such as accelerate() and brake(). A particular object myCar could be a red car with a 1.6-litre engine.
例如,类 Car 可以有属性 colour、engineSize 和 registration,以及方法 accelerate() 和 brake()。特定对象 myCar 可以是一辆红色、1.6 升发动机的汽车。
In most languages, a class is declared using the keyword class, and an object is created by calling a special method called a constructor. The constructor initialises the object’s attributes.
在大多数语言中,类使用关键字 class 声明,对象通过调用称为构造函数的特殊方法来创建。构造函数负责初始化对象的属性。
3. Encapsulation | 封装
Encapsulation is the practice of hiding the internal state of an object and requiring all interaction to occur through public methods. This protects data from accidental corruption and makes the class easier to change without affecting the rest of the program.
封装是隐藏对象内部状态并要求所有交互通过公共方法进行的做法。这可以保护数据免受意外破坏,并使类的修改不会影响程序的其他部分。
Attributes are typically declared as private, meaning they can only be accessed within the class. Public getter and setter methods allow controlled read and write access. In Python, privacy is indicated by a single underscore convention, whereas Java uses the private keyword.
属性通常声明为私有,意味着只能在类内部访问。公共的 getter 和 setter 方法允许受控的读写访问。在 Python 中,隐私通过单下划线约定表示,而 Java 使用 private 关键字。
A bank account class demonstrates encapsulation: the balance is private, and deposits must go through a deposit() method that validates the amount. Directly setting account.balance = -100 would be prevented in a well-encapsulated design.
银行账户类体现了封装:余额是私有的,存款必须通过 deposit() 方法进行并验证金额。在封装良好的设计中,直接设置 account.balance = -100 会被阻止。
4. Inheritance | 继承
Inheritance allows a new class to be based on an existing class. The new class, called a subclass or derived class, inherits the attributes and methods of the parent class, known as the superclass or base class. This promotes code reuse.
继承允许新类基于现有类进行定义。新类称为子类或派生类,继承父类(超类或基类)的属性和方法。这促进了代码重用。
A subclass can add its own attributes and methods, and it can also override inherited methods to provide specialised behaviour. For example, a SportsCar
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课程辅导,国外大学本科硕士研究生博士课程论文辅导