IB WJEC Computer Science: Object-Oriented Programming Key Concepts | IB WJEC 计算机:面向对象 考点精讲

📚 IB WJEC Computer Science: Object-Oriented Programming Key Concepts | IB WJEC 计算机:面向对象 考点精讲

Object-oriented programming (OOP) forms the backbone of modern software design and is a key assessed topic in both IB and WJEC Computer Science specifications. Mastering OOP requires understanding how classes, objects, encapsulation, and polymorphism work together to create robust, reusable code. This article unpacks every essential concept you need, pairing each explanation in English and Chinese, complete with practical examples and diagrams where helpful.

面向对象编程(OOP)是现代软件设计的基石,也是 IB 和 WJEC 计算机科学考试中的核心考查内容。掌握 OOP 需要理解类、对象、封装和多态如何协同工作来构建健壮、可复用的代码。本文逐一讲解所有关键概念,每个释义均搭配中英文对照,并辅以实例和必要的示意图。


1. The OOP Paradigm | 面向对象范式

Procedural programming organises code around functions and sequences of actions. Object-oriented programming instead models real‑world entities as objects that bundle data and the operations that manipulate that data. In IB and WJEC syllabuses, OOP is contrasted with procedural and declarative paradigms, and you must be able to explain its advantages: modularity, reusability, and easier maintenance.

过程式编程围绕函数和操作序列组织代码。面向对象编程则将现实世界的事物建模为对象,对象将数据以及操作这些数据的方法绑定在一起。在 IB 和 WJEC 课程大纲中,需要将 OOP 与过程式、声明式范式进行比较,并且必须能够解释其优势:模块化、可重用性和易于维护。


2. Classes and Objects | 类与对象

A class is a blueprint that defines the attributes (fields) and behaviours (methods) an object will have. An object is a specific instance of a class, with its own state stored in instance variables. For example, a Car class may declare colour and speed as attributes; the object myCar is one particular car with colour ‘red’ and speed 0.

类是定义对象属性和行为的蓝图。对象是类的具体实例,拥有自己的状态,存储在实例变量中。例如,一个 Car 类可以声明 colour 和 speed 作为属性;对象 myCar 则是一辆具体的汽车,颜色为“红色”,速度为 0。

IB questions often ask you to identify classes from a scenario (e.g., school management system: Student, Teacher, Course) and then instantiate objects. Memorise the distinction: class is the type, object is the value.

IB 考题经常要求从场景中识别出类(例如学校管理系统:Student、Teacher、Course),然后实例化对象。请牢记区别:类是类型,对象是值。


3. Encapsulation and Data Hiding | 封装与数据隐藏

Encapsulation bundles the data and methods that work on that data within a single unit, while data hiding restricts direct access to an object’s internal state. In most OOP languages, this is achieved using access modifiers like private and providing public getter and setter methods. It protects integrity: a BankAccount balance should not be set to a negative value directly; instead a withdraw() method validates the amount.

封装将数据和操作数据的方法捆绑在一个单元内,而数据隐藏则限制对对象内部状态的直接访问。在大多数 OOP 语言中,这通过 private 等访问修饰符并搭配公共的 getter 和 setter 方法来实现。它保护数据完整性:BankAccount 的余额不应被直接设为负值,而应通过 withdraw() 方法验证金额。

WJEC mark schemes frequently award marks for stating that encapsulation reduces dependencies and prevents accidental corruption of data. Practise writing class definitions where all fields are private.

WJEC 评分方案常对“封装能减少依赖、防止数据意外损坏”这一陈述给分。请练习编写所有字段均为 private 的类定义。


4. Inheritance: ‘is-a’ Relationships | 继承:“is-a”关系

Inheritance allows a new class (subclass) to derive properties and methods from an existing class (superclass), establishing an ‘is-a’ relationship. For instance, a Dog class inherits from Animal because a dog is an animal. The subclass can override superclass methods, add new fields, and call the superclass constructor using super().

继承允许新类(子类)从已有类(父类)派生属性和方法,从而建立“is-a”关系。例如,Dog 类继承自 Animal,因为狗是一种动物。子类可以重写父类方法、添加新字段,并使用 super() 调用父类构造函数。

Both IB and WJEC require you to recognise inheritance in UML class diagrams (hollow triangle arrow pointing to the superclass). Be prepared to discuss the benefit: code reuse, and the risk: tight coupling.

IB 和 WJEC 都要求能识别 UML 类图中的继承关系(空心三角箭头指向父类)。要准备好讨论其优点:代码复用;以及风险:紧密耦合。


5. Polymorphism: Many Forms | 多态:多种形态

Polymorphism means ‘many forms’ and enables a single interface to control access to objects of different types. The most common form is override polymorphism, where a subclass provides a different implementation of a method defined in its superclass. For example, a Shape superclass may declare draw(), and Circle and Square override it differently.

多态意为“多种形态”,它使一个统一接口能够操控不同类型的对象。最常见的形式是重写多态,即子类对父类定义的方法提供不同的实现。例如,父类 Shape 声明 draw(),而 Circle 和 Square 分别以不同方式重写该方法。

You must distinguish between compile-time (overloading) and run-time polymorphism (overriding). IB Paper 2 often includes a trace table where a base-class reference points to a derived-class object, demonstrating dynamic method dispatch.

必须区分编译时多态(重载)和运行时多态(重写)。IB Paper 2 常出现一种跟踪表题目:基类引用指向派生类对象,从而演示动态方法调度。


6. Abstraction and Abstract Classes | 抽象与抽象类

Abstraction hides complex implementation details and exposes only essential features. In code, an abstract class cannot be instantiated; it serves as a template for subclasses. It may contain abstract methods (without a body) that subclasses are forced to implement. For example, an abstract Vehicle class with abstract startEngine() forces Car and Motorbike to provide their own version.

抽象隐藏复杂的实现细节,只暴露必要特性。在代码中,抽象类不能被实例化,它作为子类的模板。它可包含抽象方法(无方法体),强制子类实现。例如,抽象类 Vehicle 带有 abstract startEngine() 方法,将强制 Car 和 Motorbike 提供各自的实现。

Remember: abstract classes represent incomplete concepts. They often appear in exam questions about designing a class hierarchy. WJEC may ask you to tick whether a class should be abstract based on a scenario.

请记住:抽象类代表不完整的概念。它们经常出现在关于设计类层次结构的试题中。WJEC 可能要求根据场景判断某个类是否应设为抽象。


7. Interfaces | 接口

An interface defines a contract: a set of method signatures that any implementing class must provide. Unlike abstract classes, interfaces contain no instance fields and (before modern Java) no method bodies. A class can implement multiple interfaces, achieving a form of multiple inheritance. In IB, the Comparable interface is a classic example: implementing compareTo() allows sorting of custom objects.

接口定义了一份合约:一组方法签名,任何实现该接口的类都必须提供这些方法。与抽象类不同,接口不包含实例字段,并且(在早期 Java 版本中)不含方法体。一个类可以实现多个接口,从而实现一种多重继承。IB 考试中,Comparable 接口是一个经典例子:实现 compareTo() 即可对自定义对象排序。

Exam tip: use interfaces to model ‘can-do’ relationships (e.g., Serializable, Runnable). The UML representation is a dashed arrow with a hollow triangle, or a lollipop notation.

备考提示:用接口来建模“能做什么”的关系(例如 Serializable、Runnable)。在 UML 中,接口的实现用带空心三角的虚线箭头表示,或采用棒棒糖符号。


8. Constructors and Destructors | 构造函数与析构函数

A constructor is a special method that initialises a new object. It has the same name as the class and no return type. Overloaded constructors allow different ways to instantiate objects. A destructor (or finalizer) cleans up resources when an object is destroyed, though in languages with automatic garbage collection (like Java and C#) you rarely write explicit destructors.

构造函数是用于初始化新对象的特殊方法。它与类同名且无返回类型。重载构造函数允许以不同方式实例化对象。析构函数(或终结器)在对象销毁时清理资源,但在具备自动垃圾回收机制的语言(如 Java 和 C#)中,很少需要显式编写析构函数。

Both exams may ask you to spot missing constructors or identify the correct call to super() in a subclass constructor. Always ensure a constructor fully initialises the object’s state.

两种考试都可能要求找出缺失的构造函数,或识别子类构造函数中正确的 super() 调用。请始终确保构造函数完整地初始化了对象的状态。


9. Access Modifiers | 访问修饰符

Modifier Class Package Subclass World
public ✓ ✓ ✓ ✓
protected ✓ ✓ ✓ ✗
default ✓ ✓ ✗ ✗
private ✓ ✗ ✗ ✗

Access modifiers control the visibility of classes, methods, and fields. WJEC often uses a table like the one above. Remember encapsulation best practice: fields private, methods public unless there is a good reason to widen visibility.

访问修饰符控制类、方法和字段的可见性。WJEC 考试常使用类似上表的总结。请牢记封装的最佳实践:字段设为 private,方法设为 public,除非有充分的理由扩大可见性。


10. Static vs Instance Members | 静态成员与实例成员

A static member belongs to the class itself, not to any individual object. All instances share the same static variable, and static methods can be called without creating an object. The Math.PI constant and Math.pow() method are excellent examples. Instance members, on the other hand, require an object and can access instance-specific data.

静态成员属于类本身,而不属于任何一个对象。所有实例共享同一个静态变量,静态方法无需创建对象即可调用。Math.PI 常量和 Math.pow() 方法就是极佳的示例。实例成员则需要通过对象访问,并能操作特定实例的数据。

In an exam, you might be asked to highlight the memory model: a static variable is stored in a single memory location, whereas each object has its own copy of instance variables. This relates to the static keyword used for main() methods.

考试中可能会要求解释内存模型:静态变量存放在单一内存位置,而每个对象都拥有自己的实例变量副本。这与 main() 方法使用的 static 关键字相关。


11. Composition and Aggregation | 组合与聚合

When inheritance represents ‘is-a’, composition represents ‘has-a’. A Car has an Engine; if the car is destroyed, the engine normally is too (strong ownership – composition). Aggregation is a weaker ‘has-a’ where the contained object can exist independently, like a Library and Book. UML distinguishes these with a filled diamond (composition) versus an empty diamond (aggregation).

如果继承表示“is-a”,那么组合表示“has-a”。一辆 Car 有一个 Engine;如果汽车销毁,引擎通常也随之销毁(强拥有关系——组合)。聚合则是一种更弱的“has-a”,例如 Library 和 Book,被包含对象可以独立存在。UML 中通过实心菱形(组合)和空心菱形(聚合)加以区分。

Favour composition over inheritance is a well-known design principle that IB examiners love. It reduces tight coupling and makes systems easier to modify.

“优先使用组合而非继承”是一条著名的设计原则,深受 IB 出题人青睐。它减少了紧密耦合,使系统更易于修改。


12. OOP Design Principles and Exam Preparation | OOP 设计原则与备考

Apart from the SOLID principles, you should be able to discuss cohesion (a class should have a single, well-defined purpose) and coupling (minimise dependencies between classes). High cohesion and loose coupling are the hallmarks of a well-designed OOP system.

除了 SOLID 原则之外,还应该能讨论内聚(一个类应具有单一的、明确的职责)和耦合(最小化类之间的依赖)。高内聚和松耦合是设计良好的 OOP 系统的标志。

For final revision, draw UML diagrams for every pattern you study, practise hand-tracing polymorphic method calls, and write short definitions of the key terms. The bilingual explanations above mirror the depth required for top marks. Good luck!

期末复习时,请为每种模式绘制 UML 图、练习手动跟踪多态方法调用,并写出关键术语的简短定义。上文中英对照的讲解深度足以帮助你冲击高分。祝考试顺利!

Published by TutorHao | 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