📚 IGCSE WJEC Computer Science: Object Oriented Programming Key Points | IGCSE WJEC 计算机:面向对象 考点精讲
Object-oriented programming (OOP) is a fundamental paradigm in computer science, and it appears in the WJEC IGCSE syllabus. Understanding OOP concepts such as classes, objects, inheritance, and encapsulation is essential for both theoretical exams and practical programming tasks. In this revision guide, we break down every key point you need to know.
面向对象编程 (OOP) 是计算机科学中的一个基本范式,在 WJEC IGCSE 大纲中有所体现。理解类、对象、继承和封装等 OOP 概念对于理论考试和上机编程都至关重要。本复习指南将为你逐一精讲每个必考点。
1. What is Object Oriented Programming? | 什么是面向对象编程?
OOP is a programming approach that organises software design around data, or objects, rather than functions and logic. An object contains data in the form of attributes (fields) and behaviour in the form of methods. This mirrors real-world entities. For example, a 'Car' object might have attributes like colour and speed, and methods like accelerate() and brake(). The core idea is to model programs as a collection of interacting objects.
OOP 是一种以数据(即对象)而非函数和逻辑来组织软件设计的编程方法。对象包含数据(属性/字段)和行为(方法)。这模拟了现实世界的实体。例如,‘汽车’对象可以有颜色和速度等属性,以及 accelerate() 和 brake() 等方法。其核心思想是将程序建模为一系列相互交互的对象。
2. Classes and Objects | 类与对象
A class is a blueprint or template for creating objects. It defines the attributes and methods that objects of that class will have. An object is an instance of a class: it occupies memory and holds its own copy of the attribute values. You can create many objects from the same class. For example, a class Student may have attributes name and grade, and a method displayInfo(). Multiple Student objects (e.g., alice, bob) can be instantiated with different data: alice.name may be "Alice", while bob.name is "Bob".
类是创建对象的蓝图或模板。它定义了该类对象将具有的属性和方法。对象是类的实例:它占用内存并持有自己的属性值副本。你可以从同一个类创建多个对象。例如,Student 类可以有 name 和 grade 属性,以及 displayInfo() 方法。可以实例化多个学生对象(如 alice、bob),它们具有不同的数据:alice.name 可能是 "Alice",而 bob.name 是 "Bob"。
3. Attributes and Methods | 属性与方法
Attributes store the state of an object. They are variables declared inside the class. In Python, attributes are often defined inside the constructor using self.attribute_name. Methods define the behaviour of an object. They are functions defined inside the class that can access and modify the object's attributes. For instance, a BankAccount class might have an attribute balance and methods deposit(amount) and withdraw(amount). To access an attribute or call a method, we use dot notation: myAccount.balance or myAccount.deposit(100).
属性存储对象的状态。它们是类内部声明的变量。在 Python 中,属性通常在构造器内通过 self.attribute_name 定义。方法定义了对象的行为。它们是类内部定义的函数,可以访问和修改对象的属性。例如,一个 BankAccount 类可能具有属性 balance 以及方法 deposit(amount) 和 withdraw(amount)。要访问属性或调用方法,我们使用点表示法:myAccount.balance 或 myAccount.deposit(100)。
4. Constructor Method | 构造器方法
A constructor is a special method that is automatically called when an object is instantiated (created). Its main job is to initialise the object's attributes to valid starting values. In Python, the constructor is __init__(self, ...). In Java or C#, it shares the class name. For a Book class, the constructor might be def __init__(self, title, author): and set self.title = title, self.author = author. When you write b1 = Book("1984", "Orwell"), the constructor runs, and the object is ready to use.
构造器是一种特殊方法,在对象被实例化(创建)时自动调用。其主要工作是将对象的属性初始化为有效的起始值。在 Python 中,构造器是 __init__(self, ...)。在 Java 或 C# 中,它与类同名。对于 Book 类,构造器可以是 def __init__(self, title, author): 并设置 self.title = title、self.author = author。当你写下 b1 = Book("1984", "Orwell") 时,构造器运行,对象即可使用。
5. Encapsulation and Access Modifiers | 封装与访问修饰符
Encapsulation means bundling the data (attributes) and the methods that operate on the data into a single unit (the class), and restricting direct access to some of an object's internal components. This is achieved using access modifiers. In WJEC, you need to know private and public. A private attribute (prefixed with two underscores in Python like __secret; private keyword in Java) can only be accessed from within the class. Public members can be accessed from anywhere. Encapsulation protects data integrity by preventing external code from accidentally changing an object's state to an invalid value.
封装意味着将数据(属性)和操作该数据的方法捆绑到一个单一单元(类)中,并限制对对象某些内部组件的直接访问。这是通过访问修饰符实现的。在 WJEC 中,你需要了解 private(私有) 和 public(公共)。私有属性(Python 中用双下划线前缀如 __secret;Java 中用 private 关键字)只能在类内部访问。公共成员可以从任何地方访问。封装通过防止外部代码意外将对象状态更改为无效值来保护数据完整性。
6. Inheritance | 继承
Inheritance allows a new class (subclass or derived class) to acquire the attributes and methods of an existing class (superclass or base class). The subclass can reuse the superclass's code, and it can also add its own specific features or override inherited methods. This promotes code reusability. For example, a superclass Vehicle has attributes speed and method move(). A subclass Car inherits from Vehicle, so a Car object automatically has speed and move(), and can extend them with a numberOfDoors attribute. In a class diagram, inheritance is shown with a hollow triangle arrow pointing to the superclass.
继承允许新类(子类或派生类)获取现有类(超类或基类)的属性和方法。子类可以重用超类的代码,还可以添加自己的特定功能或重写继承的方法。这促进了代码重用性。例如,超类 Vehicle 具有属性 speed 和方法 move()。子类 Car 继承自 Vehicle,因此 Car 对象自动拥有 speed 和 move(),并且可以用 numberOfDoors 属性对其进行扩展。在类图中,继承用一个指向超类的空心三角箭头表示。
7. Polymorphism | 多态
Polymorphism means "many forms". It allows objects of different classes to be treated as objects of a common superclass, with the actual behaviour determined at runtime. The most typical form is method overriding: a subclass provides its own version of a method that is already defined in the superclass. For example, Shape has a method draw(). Subclasses Circle and Rectangle each override draw() to produce the correct shape. A program can then call draw() on a Shape reference, and the correct override will execute. This makes code more flexible and extensible.
多态意为“多种形态”。它允许不同类的对象被视为共同超类的对象,实际行为在运行时确定。最典型的形式是方法重写:子类提供自己对超类中已定义方法的实现版本。例如,Shape 有一个方法 draw()。子类 Circle 和 Rectangle 各自重写 draw() 以绘制正确的图形。然后程序可以在 Shape 引用上调用 draw(),而正确的重写方法会执行。这使得代码更加灵活和可扩展。
8. Getter and Setter Methods | 取值器与赋值器方法
Getters (accessors) and setters (mutators) are methods that provide controlled read and write access to private attributes. A getter simply returns the value of an attribute. A setter updates the attribute after performing validation, ensuring the new value is acceptable. For instance, a Student class with a private attribute __grade can have getGrade() returning self.__grade and setGrade(newGrade) checking that 0 <= newGrade <= 100 before assignment. This upholds encapsulation while allowing necessary outside interaction.
取值器(getter)和赋值器(setter)是提供对私有属性受控读写访问的方法。取值器仅返回属性的值。赋值器则在执行验证后更新属性,确保新值可接受。例如,具有私有属性 __grade 的 Student 类可以有 getGrade() 返回 self.__grade,以及 setGrade(newGrade) 在赋值前检查 0 <= newGrade <= 100。这维护了封装性,同时允许必要的外部交互。
9. Static Members | 静态成员
Static attributes and static methods belong to the class itself, not to any individual object. They are shared across all instances of the class. A static attribute can be used to keep a count of how many objects have been created, for example. In Python, you define a class variable directly inside the class (rather than with self) for a static attribute, and decorate methods with @classmethod or @staticmethod. A static method can be called using the class name, without needing to instantiate an object: MyClass.staticMethod(). These are useful for utility functions that do not depend on instance data.
静态属性和静态方法属于类本身,而非任何个别对象。它们在类的所有实例之间共享。例如,静态属性可用于记录已创建对象的数量。在 Python 中,你直接在类内部定义类变量(而非使用 self)作为静态属性,并用 @classmethod 或 @staticmethod 装饰方法。静态方法可以使用类名调用,无需实例化对象:MyClass.staticMethod()。这些对于不依赖实例数据的工具函数非常有用。
10. Benefits of OOP | 面向对象的好处
OOP offers several advantages that the WJEC exam may ask you to identify and explain:
- Code reusability – Inheritance allows developers to build new classes on top of existing ones, reducing duplication.
- Modularity – Objects are independent entities; changes to one class do not necessarily break others.
- Data security – Encapsulation hides data and protects it from unauthorised access or accidental modification.
- Maintainability – Well-designed classes are easier to debug and update.
- Real-world modelling – OOP maps naturally to real-world objects, making design intuitive.
OOP 提供了若干优势,WJEC 考试可能要求你识别并解释这些优势:
- 代码重用性 – 继承允许开发人员在现有类的基础上构建新类,减少重复。
- 模块化 – 对象是独立的实体;对一个类的更改不一定破坏其他类。
- 数据安全性 – 封装隐藏数据并保护其免受未授权访问或意外修改。
- 可维护性 – 设计良好的类更容易调试和更新。
- 现实世界建模 – OOP 自然地映射到现实世界对象,使设计更直观。
11. Representing Classes with UML | 使用 UML 表示类
Unified Modelling Language (UML) class diagrams are a standard way to visualise OOP designs. A class is drawn as a rectangle divided into three compartments: class name at the top, attributes in the middle, and methods at the bottom. Visibility is indicated by a '+' for public and a '–' for private before the attribute or method name. Inheritance is shown as a solid line with a hollow triangle arrowhead pointing from the subclass to the superclass. You may be asked to interpret or sketch such diagrams to show relationships between classes in the exam.
统一建模语言 (UML) 类图是可视化 OOP 设计的标准方式。类被绘制为一个分为三个隔间的矩形:顶部为类名,中间为属性,底部为方法。可见性通过在属性或方法名称前加 '+' 表示公共,'–' 表示私有来表明。继承用一条带空心三角箭头的实线表示,箭头从子类指向超类。在考试中,你可能需要解读或绘制此类图以展示类之间的关系。
Published by TutorHao | Computer Science Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)

或截图后用「扫一扫 → 相册」选择本图