Object-Oriented Programming in Edexcel A-Level Computer Science | Edexcel A-Level 计算机科学中的面向对象编程

📚 Object-Oriented Programming in Edexcel A-Level Computer Science | Edexcel A-Level 计算机科学中的面向对象编程

Object-oriented programming (OOP) is a fundamental paradigm in Edexcel A-Level Computer Science. It allows programs to be modelled around real-world entities, making code more modular, reusable and easier to maintain. In the exam, you need to understand classes, objects, encapsulation, inheritance, polymorphism, constructors and instantiation, as well as be able to interpret and write simple OOP code.

面向对象编程 (OOP) 是 Edexcel A-Level 计算机科学中的核心范式。它让程序围绕现实世界中的实体建模,使代码更加模块化、可复用且易于维护。在考试中,你需要掌握类、对象、封装、继承、多态、构造函数和实例化,并能阅读和编写简单的 OOP 代码。


1. What is Object-Oriented Programming? | 什么是面向对象编程?

Object-oriented programming is a programming paradigm that organises software design around data, or objects, rather than functions and logic. An object is a self-contained entity that contains both data and the methods that operate on that data. This contrasts with procedural programming, where data and procedures are separate. OOP models real-world entities such as students, bank accounts or cars, making it easier to conceptualise and maintain complex systems.

面向对象编程是一种围绕数据(对象)而非函数和逻辑来组织软件设计的编程范式。对象是自包含的实体,既包含数据,也包含操作这些数据的方法。这与过程式编程不同,在过程式编程中数据和过程是分离的。OOP 对学生、银行账户或汽车等现实世界实体进行建模,使复杂系统更容易理解和维护。

In Edexcel exams, you may be asked to compare OOP with other paradigms such as procedural programming. Key points to remember are that OOP bundles data and behaviour together and supports reuse through inheritance. The real-world modelling aspect is often assessed in short-answer questions.

在 Edexcel 考试中,你可能会被要求将 OOP 与过程式编程等其它范式进行比较。需要记住的关键点是 OOP 将数据和行为捆绑在一起,并通过继承支持复用。现实世界建模这一特点经常在简答题中考查。


2. Classes and Objects | 类与对象

A class is a blueprint or template for creating objects. It defines the attributes (data) and methods (behaviour) that objects of that class will have. For example, a class Car might define attributes such as colour, make and currentSpeed, and methods such as accelerate() and brake(). An object is a specific instance of a class. If Car is the blueprint, then myCar = Car(“red”, “Toyota”, 0) creates a concrete object with those values.

类是创建对象的蓝图或模板。它定义了该类的对象将具有的属性(数据)和方法(行为)。例如,Car 类可以定义颜色、品牌和当前速度等属性,以及 accelerate() 和 brake() 等方法。对象是类的具体实例。如果说 Car 是蓝图,那么 myCar = Car(“red”, “Toyota”, 0) 就创建了一个具有这些值的具体对象。

It is important not to confuse a class with an object. A class exists at design time as a piece of code, while an object exists at run time and occupies memory. In a class diagram, the class name is usually shown in the top compartment, attributes in the middle, and methods at the bottom.

重要的是不要将类与对象混淆。类在设计时作为一段代码存在,而对象在运行时存在并占用内存。在类图中,类名通常显示在顶部框中,属性在中间,方法在底部。


3. Attributes and Methods | 属性与方法

Attributes are the data stored inside an object. They represent the state of the object and are often called fields or properties. Methods are functions defined inside a class that describe the behaviours an object can perform. In Edexcel exams, you may be asked to identify attributes and methods from a class diagram or a code snippet. Remember: attributes are nouns, methods are verbs. For example, in a Student class, name, age and grade are attributes, while enrol(), sitExam() and getGrade() are methods.

属性是存储在对象内部的数据。它们表示对象的状态,通常也被称为字段或属性。方法是在类内部定义的函数,描述对象能够执行的行为。在 Edexcel 考试中,你可能需要从类图或代码片段中识别属性和方法。记住:属性是名词,方法是动词。例如,在 Student 类中,name、age 和 grade 是属性,而 enrol()、sitExam() 和 getGrade() 是方法。

Methods often use the object’s attributes to produce results or change state. For instance, an accelerate() method might increase the currentSpeed attribute by a fixed amount. Some methods return a value, while others simply perform an action and return nothing.

方法通常使用对象的属性来产生结果或改变状态。例如,accelerate() 方法可能会将 currentSpeed 属性增加一个固定值。有些方法返回值,而另一些方法仅执行操作并不返回任何内容。


4. Constructors and Instantiation | 构造函数与实例化

A constructor is a special method that is called automatically when an object is created. It usually initialises the object’s attributes. In Python, the constructor is __init__; in Java, it has the same name as the class. Instantiation is the process of creating an object from a class using the constructor. For example, Student s1 = new Student(“Alice”, 17); in Java instantiates a Student object and calls the constructor to set initial values. The keyword new is used in Java, while Python simply calls the class name: s1 = Student(“Alice”, 17).

构造函数是一种特殊的方法,在创建对象时会自动调用。它通常用于初始化对象的属性。在 Python 中,构造函数是 __init__;在 Java 中,它与类同名。实例化是使用构造函数从类创建对象的过程。例如,Student s1 = new Student(“Alice”, 17); 在 Java 中实例化一个 Student 对象并调用构造函数设置初始值。Java 中使用关键字 new,而 Python 直接调用类名:s1 = Student(“Alice”, 17)。

A constructor may have parameters to pass initial values, or it may be a default constructor with no parameters. If no constructor is written, some languages provide a default one that sets attributes to null or zero. In Edexcel pseudocode, the constructor is often written as a procedure called new or init.

构造函数可以带有参数以传递初始值,也可以是无参数的默认构造函数。如果没有编写构造函数,某些语言会提供一个默认构造函数,将属性设置为 null 或零。在 Edexcel 伪代码中,构造函数通常写成一个名为 new 或 init 的过程。


5. Encapsulation and Access Modifiers | 封装与访问修饰符

Encapsulation is the technique of hiding an object’s internal state and requiring all interaction to happen through methods. This protects data from being changed in unexpected ways. Access modifiers control the visibility of attributes and methods. Common modifiers are public (accessible from anywhere), private (only accessible inside the class) and protected (accessible inside the class and its subclasses). In Python, the convention is to use a single underscore _ for protected and double underscore __ for private, though it is not strictly enforced. Encapsulation helps build robust code by enforcing a controlled interface.

封装是一种隐藏对象内部状态并要求所有交互都必须通过方法进行的技术。这可以保护数据不被意外修改。访问修饰符控制属性和方法的可见性。常见的修饰符包括 public(任何地方均可访问)、private(只能在类内部访问)和 protected(类及其子类内部可访问)。在 Python 中,约定使用单下划线 _ 表示受保护,双下划线 __ 表示私有,但这并不是严格强制执行的。封装通过强制受控接口来构建健壮的代码。

Access modifier Python convention Visibility
public no underscore everywhere
protected _attribute class and subclasses
private __attribute class only

In practice, private attributes are accessed through public getter and setter methods, such as getName() and setName(). This allows validation and maintains control over how data is modified.

在实际中,私有属性通过公共的 getter 和 setter 方法访问,例如 getName() 和 setName()。这样可以在修改数据时进行验证并保持控制。


6. Inheritance | 继承

Inheritance allows a new class (subclass) to acquire the attributes and methods of an existing class (superclass). The subclass can add new attributes and methods or override inherited ones. This promotes code reuse and models ‘is-a’ relationships. For example, a class Dog can inherit from a class Animal, so Dog gets attributes such as name and age and methods such as eat() and sleep(), and can add a bark() method. In Java, inheritance uses the keyword extends; in Python, the parent class is placed in parentheses: class Dog(Animal).

继承允许新类(子类)获取现有类(父类)的属性和方法。子类可以添加新的属性和方法,或者重写继承的方法。这促进了代码复用并建模“是”关系。例如,Dog 类可以继承 Animal 类,因此 Dog 获得 name 和 age 等属性以及 eat() 和 sleep() 等方法,还可以添加 bark() 方法。在 Java 中,继承使用关键字 extends;在 Python 中,父类放在括号内:class Dog(Animal)。

Inheritance creates a class hierarchy. At the top is the most general superclass, and as we move down, classes become more specific. Multiple levels of inheritance are possible, but a subclass usually has only one direct parent in languages like Java to avoid complexity. The super keyword is used to call the superclass constructor or methods.

继承创建了类层次结构。顶部是最通用的父类,向下移动时,类变得越来越具体。多级继承是可能的,但在 Java 等语言中,子类通常只有一个直接父类,以避免复杂性。super 关键字用于调用父类的构造函数或方法。


7. Overriding and Polymorphism | 重写与多态

Method overriding occurs when a subclass provides a different implementation of a method that is already defined in its superclass. Polymorphism means ‘many forms’ and allows a single interface to represent different underlying types. For example, a superclass Shape may have a method area(), and subclasses Circle and Rectangle override area() with their own formulae. A polymorphic call such as shape.area() will invoke the correct version depending on the actual object type at run time. Polymorphism is often tested in Edexcel exams via code tracing questions.

方法重写发生在子类为其父类中已经定义的方法提供不同实现时。多态意为“多种形态”,它允许单个接口表示不同的底层类型。例如,父类 Shape 可以有一个 area() 方法,子类 Circle 和 Rectangle 用自己的公式重写 area()。多态调用如 shape.area() 将根据运行时实际对象类型调用正确的版本。Edexcel 考试经常通过代码跟踪题来测试多态。

Dynamic dispatch is the mechanism behind polymorphism. When a method is called on a reference variable, the actual method executed depends on the object’s type, not the reference type. This allows writing flexible code that can work with any subclass without knowing its exact type at compile time.

动态分派是多态背后的机制。当在引用变量上调用方法时,实际执行的方法取决于对象的类型,而不是引用类型。这允许编写灵活的代码,可以在不知道编译时确切类型的情况下处理任何子类。


8. Abstract Classes and Interfaces | 抽象类与接口

An abstract class is a class that cannot be instantiated directly and may contain abstract methods—methods without a body that must be implemented by subclasses. An interface is a contract that specifies a set of methods that a class must implement, without providing any implementation. In Java, abstract classes use the keyword abstract, and interfaces use interface. Python supports abstract base classes via the abc module. These constructs support design flexibility and guarantee that certain methods exist.

抽象类是不能直接实例化的类,它可以包含抽象方法——即没有方法体、必须由子类实现的方法。接口是一种契约,规定类必须实现的一组方法,但不提供任何实现。在 Java 中,抽象类使用关键字 abstract,接口使用 interface。Python 通过 abc 模块支持抽象基类。这些结构支持设计灵活性,并保证某些方法一定存在。

Abstract classes can have both concrete and abstract methods, while interfaces traditionally only declare method signatures. A class can implement multiple interfaces but typically extend only one abstract class. This distinction is useful in design questions where you need to choose the right construct for a given scenario.

抽象类可以同时具有具体方法和抽象方法,而接口传统上只声明方法签名。一个类可以实现多个接口,但通常只能继承一个抽象类。这种区别在设计题中很有用,在这些题中你需要为给定场景选择合适的结构。

Published by TutorHao | A-Level 编程 Revision Series | aleveler.com

更多咨询请联系16621398022(同微信)

Comments

屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from aleveler.com

Subscribe now to keep reading and get access to the full archive.

Continue reading