📚 Object-Oriented Programming: Classes, Objects, and Inheritance | 面向对象编程:类、对象与继承
Object-oriented programming (OOP) is a programming paradigm that organises software design around objects rather than functions and logic. In Edexcel A-Level Computer Science, you need to understand how classes act as blueprints, how objects are instantiated, and how inheritance and polymorphism support code reuse.
面向对象编程(OOP)是一种围绕对象而非函数和逻辑来组织软件设计的编程范式。在 Edexcel A-Level 计算机科学中,你需要理解类如何充当蓝图、对象如何实例化,以及继承和多态如何支持代码复用。
1. Programming Paradigms and the Rise of OOP | 编程范式与面向对象编程的兴起
A programming paradigm is a style or way of thinking about software construction. Procedural programming uses step-by-step procedures, while declarative programming states what should be achieved without specifying every control flow detail.
编程范式是一种构建软件的思维方式或风格。面向过程的编程使用逐步执行的过程,而声明式编程只说明需要实现什么,不指定每个控制流细节。
OOP introduces classes and objects so that data and the methods that operate on that data are bundled together. This makes large systems easier to model, debug, and extend.
OOP 引入了类和对象,使数据以及操作这些数据的方法被捆绑在一起。这使得大型系统更容易建模、调试和扩展。
In the Edexcel specification, you may be asked to explain why OOP is suitable for simulations, games, or GUI applications where real-world entities have clear attributes and behaviours.
在 Edexcel 大纲中,你可能会被要求解释为什么 OOP 适合模拟、游戏或 GUI 应用,因为这些场景中的现实世界实体具有清晰的属性和行为。
2. Classes and Objects: Blueprint and Instance | 类与对象:蓝图与实例
A class is a template that defines the attributes and methods common to all objects of a certain kind. An object is a concrete instance of a class, created at runtime.
类是定义某一类对象共有的属性和方法的模板。对象是类的具体实例,在运行时创建。
For example, a class Car may define attributes such as colour and speed, and methods such as accelerate() and brake(). The object myCar = Car(“red”) is one particular car with its own state.
例如,类 Car 可以定义 colour 和 speed 等属性,以及 accelerate() 和 brake() 等方法。对象 myCar = Car(“red”) 是一辆具有自身状态的特定汽车。
-
Class = blueprint or template. | 类 = 蓝图或模板。
-
Object = instance with concrete values. | 对象 = 具有具体值的实例。
-
Instantiation = the process of creating an object from a class. | 实例化 = 从类创建对象的过程。
3. Attributes: Data Stored Inside an Object | 属性:对象中存储的数据
Attributes are variables that belong to an object. They store the state of the object and can be public, private, or protected depending on the programming language and access modifiers.
属性是属于对象的变量。它们存储对象的状态,根据编程语言和访问修饰符,可以是公开的、私有的或受保护的。
In pseudocode for Edexcel, attributes are often declared at the top of a class. For example: PRIVATE colour : STRING.
在 Edexcel 的伪代码中,属性通常在类的顶部声明。例如:PRIVATE colour : STRING。
When an object is created, each attribute receives its own copy in memory, so two Car objects can have different colour values.
当对象被创建时,每个属性在内存中都会获得自己的副本,因此两个 Car 对象可以有不同的 colour 值。
4. Methods: Behaviour Defined by a Class | 方法:类定义的行为
Methods are functions defined inside a class that describe what an object can do. They can access and modify the object’s attributes.
方法是定义在类内部的函数,描述对象能够执行的操作。它们可以访问和修改对象的属性。
A method signature includes its name, parameters, and return type. In OOP, methods are invoked on an object using dot notation, such as myCar.accelerate(10).
方法签名包括其名称、参数和返回类型。在 OOP 中,方法通过点表示法在对象上调用,例如 myCar.accelerate(10)。
Methods can be public for external use or private for internal helper tasks. This distinction supports encapsulation.
方法可以是公开的供外部使用,也可以是私有的用于内部辅助任务。这种区分支持封装。
5. Encapsulation: Protecting Data with Access Modifiers | 封装:用访问修饰符保护数据
Encapsulation means hiding the internal state of an object and only allowing access through a controlled interface. In many languages this is achieved with private attributes and public getter/setter methods.
封装意味着隐藏对象的内部状态,只允许通过受控接口访问。在许多语言中,这通过私有属性和公共的 getter/setter 方法实现。
Encapsulation protects data integrity. For example, a setter for speed can reject negative values, preventing an invalid state.
封装保护数据的完整性。例如,speed 的 setter 可以拒绝负值,从而防止无效状态。
| Access modifier | 访问修饰符 | Meaning | 含义 |
|---|---|
| Public | 公开 | Accessible from anywhere | 任何地方都可访问 |
| Private | 私有 | Only accessible inside the class | 只能在类内部访问 |
| Protected | 受保护 | Accessible in the class and its subclasses | 类及其子类中可访问 |
In Edexcel pseudocode, you may be asked to rewrite code to make an attribute private and add getters/setters, so practise this pattern.
在 Edexcel 伪代码中,你可能会被要求改写代码,将属性设为私有并添加 getter/setter,因此请练习这种模式。
6. Inheritance: Reusing and Extending Classes | 继承:复用和扩展类
Inheritance allows a new class (subclass) to adopt the attributes and methods of an existing class (superclass). This promotes code reuse and establishes an “is-a” relationship.
继承允许新类(子类)采用现有类(超类)的属性和方法。这促进了代码复用,并建立了 “is-a” 关系。
A subclass can add new attributes and methods, or override inherited methods to provide specialised behaviour. For example, ElectricCar can inherit from Car and add batteryCapacity.
子类可以添加新的属性和方法,或重写继承的方法以提供专门的行为。例如,ElectricCar 可以继承自 Car 并添加 batteryCapacity。
In pseudocode, inheritance is often shown with a colon or keyword INHERITS. In Python, class ElectricCar(Car) means ElectricCar is a subclass of Car.
在伪代码中,继承通常用冒号或关键字 INHERITS 表示。在 Python 中,class ElectricCar(Car) 表示 ElectricCar 是 Car 的子类。
Be careful: inheritance should only be used when there is a genuine is-a relationship. A Car has an Engine, so engine should be an attribute, not a subclass.
注意:只有当存在真正的 is-a 关系时才应使用继承。汽车有一个引擎,因此引擎应该是属性,而不是子类。
7. Polymorphism: One Interface, Many Implementations | 多态:一个接口,多种实现
Polymorphism allows objects of different classes to respond to the same method call in different ways. This is often achieved through method overriding.
多态允许不同类的对象以不同的方式响应相同的方法调用。这通常通过方法重写来实现。
For example, a base class Shape may define a method area(), and subclasses Circle and Rectangle each override area() with their own formulas.
例如,基类 Shape 可以定义方法 area(),子类 Circle 和 Rectangle
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课程辅导,国外大学本科硕士研究生博士课程论文辅导Cancel reply