IGCSE CIE Computer Science: Object-Oriented Programming Key Points | IGCSE CIE 计算机:面向对象 考点精讲

📚 IGCSE CIE Computer Science: Object-Oriented Programming Key Points | IGCSE CIE 计算机:面向对象 考点精讲

Object-oriented programming (OOP) is a paradigm that organises software around ‘objects’ rather than functions alone. In IGCSE Computer Science, mastering OOP fundamentals helps you write modular, reusable code and prepares you for more advanced programming. This revision guide covers the key concepts you must know, illustrated with Python-style examples familiar to CIE candidates.

面向对象编程(OOP)是一种围绕“对象”而非仅限函数来组织软件的范式。在 IGCSE 计算机科学中,掌握 OOP 基础有助于编写模块化、可重用的代码,并为更高级的编程做好准备。这份复习指南涵盖了考生必须掌握的关键概念,并配有 CIE 考生熟悉的 Python 风格示例。

1. Introduction to OOP | 面向对象编程简介

OOP treats data and the operations that can be performed on that data as a single unit, called an object. This differs from procedural programming, which separates data structures and functions.

OOP 将数据以及可对这些数据执行的操作视为一个单一单元,称为对象。这不同于过程式编程,后者将数据结构和函数分开看待。

An object can represent a real-world idea, such as a ‘BankAccount’ or a ‘Student’. By modelling the world through objects, programs become more intuitive and easier to extend.

对象可以代表现实世界中的概念,例如“银行账户”或“学生”。通过对象来模拟世界,程序变得更直观、更易于扩展。

The four pillars of OOP are encapsulation, abstraction, inheritance, and polymorphism. You will encounter these throughout your course and in exam questions that ask you to explain their advantages.

OOP 的四大支柱是封装、抽象、继承和多态。在课程学习以及要求解释其优点的考试题中,你都会遇到这些概念。

Many modern languages (Python, Java, C++) support OOP, and IGCSE exam scenarios often involve designing classes with specific attributes and methods.

许多现代语言(Python、Java、C++)都支持 OOP,而 IGCSE 考试场景常涉及设计具有特定属性和方法的类。


2. Classes and Objects | 类与对象

A class is a template or blueprint that defines the structure and behaviour its objects will have. It specifies the attributes (variables) and methods (functions) that belong to the class.

类是定义其对象将具有的结构和行为的模板或蓝图。它指定了属于该类的属性(变量)和方法(函数)。

An object is a concrete instance of a class. When you instantiate a class, memory is allocated for the object, and it can store its own values for the defined attributes.

对象是类的具体实例。当你实例化一个类时,会为对象分配内存,并且它可以为所定义的属性存储自己的值。

In Python, you define a class using the class keyword followed by the class name. An object is created by calling the class name as if it were a function: my_bank = BankAccount().

在 Python 中,使用 class 关键字后跟类名来定义类。通过像调用函数一样调用类名来创建对象:my_bank = BankAccount()

Multiple objects can be created from the same class, each with their own attribute values but sharing the same method definitions. This reusability is a core strength of OOP.

可以从同一个类创建多个对象,每个对象都有各自的属性值,但共享相同的方法定义。这种可重用性是 OOP 的核心优势。

For example, class Dog: pass creates an empty class. You can still instantiate it and later add attributes dynamically, although good design defines them inside the class.

例如,class Dog: pass 创建一个空类。你仍然可以实例化它,然后动态添加属性,不过良好的设计是在类的内部定义属性。


3. Attributes and Methods | 属性与方法

Attributes store the state of an object. They can be instance attributes (unique to each object) or class attributes (shared across all objects). Methods define the behaviour of the object.

属性存储对象的状态。它们可以是实例属性(每个对象独有)或类属性(在所有对象之间共享)。方法定义了对象的行为。

In Python, instance attributes are typically initialised inside the __init__ method using self.attribute_name = value. Class attributes are defined directly inside the class body.

在 Python 中,实例属性通常在 __init__ 方法内部使用 self.attribute_name = value 进行初始化。类属性则直接在类体内定义。

Methods are functions defined inside a class. The first parameter of an instance method must be self, which refers to the current object. You use methods to read or modify an object’s state.

方法是在类内部定义的函数。实例方法的第一个参数必须是 self,它指代当前对象。你使用方法读取或修改对象的状态。

Good OOP design suggests keeping attributes private and providing public methods (getters and setters) to access them. This protects data integrity.

良好的 OOP 设计建议将属性保持私有,并提供公共方法(getter 和 setter)来访问它们。这可以保护数据的完整性。

When designing a class for an exam question, always list the relevant attributes and methods based on the problem description before writing any code.

在考试中设计类时,务必在编写任何代码之前,根据问题描述列出相关的属性和方法。


4. Constructor (__init__) | 构造函数 (__init__)

A constructor is a special method that is automatically called when a new object is created. In Python, the constructor is named __init__ and it initialises the object’s attributes.

构造函数是一种特殊的方法,会在创建新对象时自动调用。在 Python 中,构造函数名为 __init__,用于初始化对象的属性。

You can pass arguments to the constructor to set initial values at the moment of creation. For example, def __init__(self, account_holder): self.holder = account_holder.

你可以向构造函数传递参数,以便在创建时设置初始值。例如,def __init__(self, account_holder): self.holder = account_holder

If a class does not define an __init__ method, Python provides a default constructor that takes no arguments and does nothing. However, you normally need your own to set up the object’s state.

如果类未定义 __init__ 方法,Python 会提供一个不接受参数且不执行任何操作的默认构造函数。不过,你通常需要自己的构造函数来设置对象的状态。

Within the constructor, you can also perform validation to ensure the initial values are sensible, e.g., checking that a bank balance is not negative.

在构造函数内部,你还可以执行验证,以确保初始值是合理的,例如,检查银行余额不为负数。

The constructor is called only once per object. Do not confuse it with regular methods that can be invoked repeatedly.

每个对象只会调用一次构造函数。不要将其与可以反复调用的常规方法混淆。


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

Encapsulation bundles attributes and the methods that operate on them inside a class, and restricts direct external access. This hides the internal implementation details.

封装将属性以及操作它们的方法捆绑在类的内部,并限制直接的外部访问。这隐藏了内部实现细节。

In Python, there are no strict ‘private’ keywords, but conventions are used. A single leading underscore _ (e.g., _balance) indicates a protected attribute, suggesting it should not be accessed directly.

在 Python 中,没有严格的“私有”关键字,但使用了约定。单下划线 _(例如 _balance)表示受保护的属性,暗示不应直接访问。

Double leading underscores __ trigger name mangling, making the attribute harder to access accidentally from outside the class. This provides a stronger form of privacy.

双下划线 __ 会触发名称修饰,使得从类外部意外访问该属性变得更加困难。这提供了一种更强的隐私形式。

Encapsulation reduces the risk of unintended interference and makes the code easier to debug, because changes to internal attributes are controlled through well‑defined methods.

封装降低了意外干扰的风险,并使代码更容易调试,因为对内部属性的更改是通过明确定义的方法来控制的。

In exams, you may be asked to explain why using getters and setters is better than exposing attributes directly. The answer should mention data validation and flexibility.

在考试中,你可能会被要求解释为什么使用 getter 和 setter 比直接暴露属性更好。答案应提及数据验证和灵活性。


6. Inheritance | 继承

Inheritance allows a new class, called a subclass or child, to adopt the attributes and methods of an existing class, called a superclass or parent. This models hierarchical relationships.

继承允许新类(称为子类)采用现有类(称为超类或父类)的属性和方法。这模拟了层次化的关系。

In Python, you specify inheritance by placing the parent class name in parentheses after the child class name: class Student(Person):. The child automatically has all parent methods.

在 Python 中,通过在子类名后的括号中放置父类名来指定继承:class Student(Person):。子类会自动拥有父类的所有方法。

The child can extend the parent by adding new attributes or methods, and can also override parent methods to provide specialised behaviour.

子类可以通过添加新的属性或方法来扩展父类,也可以重写父类的方法以提供专门的行为。

Use inheritance when there is a clear ‘is-a’ relationship: a ‘Car’ is a ‘Vehicle’, a ‘SavingsAccount’ is a ‘BankAccount’. Misusing inheritance for ‘has-a’ relationships leads to fragile code.

当存在明确的“是”关系时使用继承:汽车“是”交通工具,储蓄账户“是”银行账户。误将继承用于“拥有”关系会导致脆弱的代码。

In IGCSE questions, you might be shown a class diagram and asked to identify the superclass, subclasses, and the attributes that each class inherits.

在 IGCSE 题目中,你可能会看到类图,并被要求识别超类、子类以及每个类所继承的属性。


7. Method Overriding and Polymorphism | 方法重写与多态

Method overriding happens when a subclass defines a method with the same name and parameters as a method in its superclass. The subclass version replaces the parent version for objects of the subclass.

当子类定义了一个与其超类中的方法名称和参数相同的方法时,就会发生方法重写。对于子类的对象,子类版本会替换父类版本。

This allows a subclass to provide its own specialised behaviour while keeping the same external interface. For example, both a ‘Dog’ and a ‘Cat’ might override a speak() method inherited from ‘Animal’.

这允许子类在保持相同外部接口的同时,提供自己专门的行为。例如,“Dog”和“Cat”都可能重写从“Animal”继承来的 speak() 方法。

Polymorphism, meaning ‘many forms’, enables objects of different classes to be treated uniformly through a common interface. A function can accept any object that has a particular method, regardless of its exact type.

Published by TutorHao | IGCSE Computer Science 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