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

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

Object-oriented programming (OOP) is a programming paradigm central to modern software development and a major topic in the IB Computer Science course. It organises code around objects that bundle data and behaviour, enabling modularity, reusability, and maintainability. This guide covers all essential OOP concepts you need to master for the IB exams, including classes, objects, encapsulation, inheritance, polymorphism, UML diagrams, and key terminology.

面向对象编程是现代软件开发的核心理式,也是 IB 计算机科学课程的重要主题。它将代码组织为同时包含数据与行为的对象,从而实现模块化、可重用性和可维护性。本指南涵盖了 IB 考试中必须掌握的全部面向对象核心概念,包括类、对象、封装、继承、多态、UML 图及关键术语。


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

Object-oriented programming models real-world entities as objects that have state (attributes) and behaviour (methods). Instead of focusing on procedures and logic, OOP emphasises the objects that interact with each other. In IB Computer Science, you are expected to understand OOP as an abstraction that helps manage complexity by creating reusable blueprints called classes.

面向对象编程将现实世界中的实体建模为具有状态(属性)和行为(方法)的对象。它不注重过程和逻辑,而是强调对象之间的交互。在 IB 计算机科学中,你需要理解 OOP 是一种抽象,通过创建称为“类”的可重用蓝图来管理复杂性。

Languages such as Java, C++ and Python support OOP. IB exam questions often ask you to define key terms or trace code snippets that illustrate object creation and method calls. Recognising the paradigm shift from procedural to object-oriented thinking is crucial for both Paper 1 and the Internal Assessment.

Java、C++ 和 Python 等语言都支持 OOP。IB 考题经常要求你定义关键术语或追踪展示对象创建和方法调用的代码片段。从面向过程到面向对象思维的范式转变,对于试卷一和内部评估都至关重要。


2. Classes and Objects | 类与对象

A class is a template or blueprint from which individual objects are created. It defines the attributes (fields) and methods that its objects will have. An object is an instance of a class, with its own unique state. For example, a Car class may have attributes like colour and speed, and methods like accelerate() and brake().

类是用于创建单个对象的模板或蓝图,它定义了对象将拥有的属性(字段)和方法。对象是类的实例,拥有自己独特的状态。例如,一个 Car 类可以定义 colourspeed 等属性,以及 accelerate()brake() 等方法。

A constructor is a special method that is automatically called when an object is instantiated. It often initialises attribute values. In IB, you need to know the difference between a default constructor (no parameters) and a parameterised constructor. You should also be able to identify accessor methods (getters) that return attribute values, and mutator methods (setters) that modify them.

构造函数是一种特殊的方法,在对象实例化时自动调用,通常用于初始化属性值。在 IB 中,你需要了解默认构造函数(无参数)和带参构造函数的区别,还应能识别返回属性值的访问器方法 (getter) 和修改属性值的修改器方法 (setter)。

Instantiation is the process of creating an object from a class using the keyword new (in Java) or similar syntax. An object’s state is stored in heap memory, while the reference variable is stored on the stack. You may be asked to outline this memory model in your exam.

实例化是使用关键字 new(在 Java 中)或类似语法从类创建对象的过程。对象的状态存储在堆内存中,而引用变量存储在栈上。考试中可能会要求你描述这个内存模型。


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

Encapsulation is the bundling of data with the methods that operate on that data, and restricting direct access to some of an object’s components. It is implemented through access modifiers such as private, public and protected. In IB exams, you must explain how encapsulation enhances security and maintainability by hiding internal state and forcing interaction through well-defined interfaces.

封装是将数据与操作数据的方法捆绑在一起,并限制对对象某些组成部分的直接访问。它通过 privatepublicprotected 等访问修饰符实现。在 IB 考试中,你必须解释封装如何通过隐藏内部状态并强制通过明确定义的接口进行交互,从而提高安全性和可维护性。

Data hiding is a direct consequence of encapsulation. Attributes are typically declared private, and controlled access is provided via public getter and setter methods. This allows validation logic to be placed inside setters, protecting object integrity. You should be able to identify violations of encapsulation in example code and suggest improvements.

数据隐藏是封装的直接结果。属性通常被声明为 private,并通过公共的 getter 和 setter 方法提供受控访问。这使得可以在 setter 中放置验证逻辑,从而保护对象完整性。你应能识别示例代码中违反封装原则的情况并提出改进建议。

Encapsulation also supports the principle of ‘information hiding’, reducing interdependencies between modules. When a class is modified internally, external code that uses its public interface remains unaffected, leading to more maintainable systems.

封装还支持“信息隐藏”原则,减少模块之间的相互依赖。当类内部被修改时,使用其公共接口的外部代码不会受到影响,从而实现更易于维护的系统。


4. Inheritance | 继承

Inheritance allows a new class (subclass or child) to derive properties and methods from an existing class (superclass or parent). This promotes code reuse and establishes a natural hierarchy. In Java, the keyword extends is used. The IB syllabus expects you to understand how inheritance supports the ‘is-a’ relationship; for example, a Dog is an Animal.

继承允许新类(子类)从现有类(超类或父类)派生出属性和方法。这促进了代码重用并建立了自然的层次结构。在 Java 中使用关键字 extends。IB 大纲要求你理解继承如何支持“是一个”关系;例如,Dog 是一个 Animal

The subclass inherits all public and protected members of the superclass, but not private members. A child can add its own attributes and methods, or override existing ones to provide specialised behaviour. The super keyword is used to call the parent constructor or access hidden members.

子类继承超类的所有 public 和 protected 成员,但不继承 private 成员。子类可以添加自己的属性和方法,或者覆盖(重写)现有方法以提供专门的行为。关键字 super 用于调用父类构造函数或访问隐藏的成员。

Constructors are not inherited, but a subclass constructor must explicitly or implicitly call a superclass constructor. The IB exam may include trace tables that involve constructor chaining. Understanding the order of constructor execution is essential for predicting program output.

构造函数不会被继承,但子类构造函数必须显式或隐式地调用超类构造函数。IB 考试可能给出涉及构造函数链的追踪表。理解构造函数的执行顺序对于预测程序输出至关重要。


5. Polymorphism | 多态

Polymorphism means “many forms” and allows objects of different classes to be treated as objects of a common superclass. The IB curriculum distinguishes between compile-time polymorphism (method overloading) and run-time polymorphism (method overriding). Overloading means multiple methods with the same name but different parameter lists within the same class. Overriding is when a subclass provides a specific implementation of a method that is already defined in its superclass.

多态意为“多种形态”,它允许不同类的对象被视为公共超类的对象。IB 课程区分了编译时多态(方法重载)和运行时多态(方法重写)。重载是指在同一类中,多个方法具有相同名称但参数列表不同。重写则是子类为其超类中已定义的方法提供特定实现。

Dynamic binding is the mechanism by which an overridden method is resolved at runtime based on the actual object type, not the reference type. This is a common focus of Paper 1 multiple-choice and structured questions. You need to be able to determine which method version executes when a superclass reference points to a subclass object.

动态绑定是一种机制,通过该机制,重写的方法在运行时根据实际对象类型(而非引用类型)进行解析。这是试卷一选择题和结构题的常见考点。你需要能够判断当超类引用指向子类对象时,执行的是哪个版本的方法。

Polymorphism increases flexibility by allowing the same interface to be used for different underlying data types. For instance, an array of Shape references can hold Circle and Rectangle objects, and calling draw() will invoke the appropriate subclass method.

多态通过允许同一接口用于不同的底层数据类型来增加灵活性。例如,Shape 类型的引用数组可以存放 CircleRectangle 对象,调用 draw() 将触发相应子类的方法。


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

An abstract class in OOP is a class that cannot be instantiated on its own and is designed to be a base class for other classes. It may contain abstract methods (with no body) that subclasses must implement, as well as concrete methods with full implementation. In the IB syllabus, you should distinguish between an abstract class and a concrete class, and recognise the use of the abstract keyword.

面向对象中的抽象类不能被直接实例化,它被设计为其他类的基类。它可以包含无方法体的抽象方法(子类必须实现),也可以包含完整实现的具体方法。根据 IB 大纲,你应区分抽象类与具体类,并识别 abstract 关键字的使用。

An interface is a completely abstract type that defines a set of method signatures without any implementation. A class implementing an interface must provide bodies for all declared methods. Interfaces support a form of multiple inheritance, since a class can implement multiple interfaces even though it may inherit from only one superclass.

接口是一种完全抽象的类型,它定义了一组没有任何实现的方法签名。实现接口的类必须为所有声明的方法提供方法体。接口支持一种多重继承的形式,因为一个类即使只能继承一个超类,却可以实现多个接口。

IB exam questions often compare abstract classes and interfaces. Key differences include: an abstract class can have instance variables and constructors; an interface cannot (before Java 8, though IB typically follows the classic definition). Both are used to achieve abstraction and specify a common protocol.

IB 考题经常要求比较抽象类与接口。主要区别包括:抽象类可以有实例变量和构造函数;接口不行(Java 8 之前,IB 通常遵循经典定义)。两者都用于实现抽象并指定公共协议。


7. Association, Aggregation and Composition | 关联、聚合与组合

Beyond inheritance, objects can be related through association, indicating a relationship between classes. Association can be unidirectional or bidirectional. In UML class diagrams, it is shown as a solid line connecting the classes. The IB may require you to interpret and draw such relationships.

除了继承,对象还可以通过关联相互联系,表明类之间的关系。关联可以是单向或双向的。在 UML 类图中,关联用连接类的实线表示。IB 可能要求你理解和绘制这类关系。

Aggregation is a special form of association representing a “has-a” relationship where the part can exist independently of the whole. For example, a Library aggregates Book objects; books can exist without the library. It is depicted by a hollow diamond on the container side.

聚合是一种特殊的关联形式,表示“拥有”关系,其中部分可以独立于整体存在。例如,Library 聚合并包含 Book 对象;书可以脱离图书馆而存在。在图中,容器一侧用空心菱形表示。

Composition is a stronger form of aggregation implying ownership, where the part cannot exist independently. If the whole is destroyed, its parts are destroyed as well. A House composed of Room objects is a classic example. Composition is drawn with a filled diamond on the whole side.

组合是一种更强的聚合形式,意味着所有权关系,其中部分不能独立存在。如果整体被销毁,其组成部分也会被销毁。由 Room 对象组成的 House 是典型例子。组合在整体一侧用实心菱形绘制。

Relationship 符号 生命周期
Association Solid line 独立
Aggregation 空心菱形 部分可独立
Composition 实心菱形 部分依附于整体

8. UML Class Diagrams | UML 类图

Unified Modeling Language (UML) class diagrams are a standard way to visualise a system’s classes, their attributes, methods, and relationships. In the IB course, you must be able to draw and interpret simplified class diagrams. A class box is divided into three compartments: the class name (top), attributes (middle), and methods (bottom).

统一建模语言 (UML) 类图是一种可视化系统类、属性、方法及其关系的标准方式。在 IB 课程中,你必须能够绘制和解读简化类图。一个类图框分为三个部分:类名(顶部)、属性(中间)和方法(底部)。

Visibility modifiers are indicated by symbols: ‘+’ for public, ‘-‘ for private, and ‘#’ for protected. Attribute format is typically visibility name : type, and method format is visibility name(parameters) : returnType. IB exam rubrics often expect these details to be accurate.

可见性修饰符用符号表示:’+’ 表示 public,’-‘ 表示 private,’#’ 表示 protected。属性格式通常为 可见性 名称 : 类型,方法格式为 可见性 名称(参数) : 返回类型。IB 评分标准通常要求这些细节准确无误。

Inheritance is shown with a solid line and a hollow triangular arrow pointing to the superclass. Association is a simple solid line, possibly with multiplicities (e.g., 1..*). You should practise sketching simple diagrams from problem descriptions and explaining them in written responses.

继承用带空心三角箭头的实线表示,箭头指向超类。关联是一条简单实线,可能带有多重性标识(如 1..*)。你应练习根据问题描述绘制简单类图,并在书面回答中进行解释。


9. Overloading vs Overriding | 重载与重写辨析

This comparison is frequently tested. Overloading (compile-time polymorphism) occurs when two or more methods in the same class share the same name but have different parameter lists (order, type, or number). Return type alone is not sufficient to distinguish overloaded methods. Overriding (run-time polymorphism) happens when a subclass redefines a method inherited from its superclass, maintaining the same signature and return type.

这一比较是常考知识点。重载(编译时多态)发生在同一类中的两个或多个方法共享同一名称但参数列表不同(顺序、类型或个数)。仅靠返回类型不足以区分重载方法。重写(运行时多态)则是子类重新定义从其超类继承的方法,保持相同的方法签名和返回类型。

Overloading provides flexibility by allowing a method to handle different input variations without changing the method name. Overriding enables a subclass to offer a specialised version of a general behaviour. In IB, you may be asked to recognise legal and illegal overloading/overriding examples in code snippets.

重载通过允许一个方法处理不同的输入变体而不改变方法名,提供了灵活性。重写则使子类能够提供通用行为的专用版本。在 IB 中,你可能需要辨别代码片段中合法与非法的重载/重写示例。

A key rule: overriding methods cannot reduce the visibility of the inherited method (e.g., you cannot override a public method with a protected one). They also cannot throw broader checked exceptions. Understanding these rules helps you avoid common pitfalls in the IA and exams.

一个关键规则:重写方法不能降低所继承方法的可见性(例如,不能将 public 方法重写为 protected)。它们也不能抛出更宽泛的检查型异常。理解这些规则有助于你避免内部评估和考试中的常见错误。


10. Advantages and Disadvantages of OOP | 面向对象的优缺点

OOP offers substantial advantages that justify its widespread use in industry and its emphasis in IB. Encapsulation improves security and modularity. Inheritance promotes code reuse and logical hierarchy. Polymorphism increases flexibility in integrating new classes. The resulting software tends to be easier to maintain and extend, aligning with real-world modelling.

面向对象提供了重要优势,从而证明了其在工业界的广泛使用和在 IB 中的侧重地位。封装提高了安全性和模块化程度。继承促进了代码复用和逻辑层次结构。多态增加了集成新类的灵活性。由此产生的软件往往更易于维护和扩展,并与现实世界建模契合。

However, OOP is not without drawbacks. Programs can be larger and more memory-intensive due to the overhead of objects. Steeper learning curves often challenge beginners struggling with abstraction. Overuse of inheritance can lead to deep, rigid hierarchies that are difficult to refactor. A balanced approach is essential.

然而,OOP 并非没有缺点。由于对象的开销,程序可能更大且更耗内存。陡峭的学习曲线常常给初学者带来抽象理解上的困难。过度使用继承可能导致过深、僵化的层次结构,难以重构。保持适度平衡至关重要。

IB exam essays occasionally ask you to evaluate OOP against procedural programming. You should be prepared to discuss both strengths and limitations, referring to concrete scenarios like code maintainability, development time, and runtime efficiency.

IB 考试中的论文题有时会要求你评价 OOP 相对于面向过程编程的优劣。你应准备讨论其优势和局限,并提到代码可维护性、开发时间和运行效率等具体场景。


11. Exam Tips and Key Terminology Summary | 备考技巧与核心术语总结

For success in the IB Computer Science exam, focus on precise definitions and the ability to apply concepts to small code examples. Be ready to identify classes, objects, constructors, accessor/mutator methods, and indicators of encapsulation. Practice tracing code that involves inheritance and polymorphism, determining which method is called at runtime.

要在 IB 计算机科学考试中取得成功,请专注于精确定义以及将概念应用于小型代码示例的能力。准备好识别类、对象、构造函数、访问器/修改器方法以及封装的标志。练习追踪涉及继承和多态的代码,判断运行时调用了哪个方法。

Master UML notation, especially the difference between aggregation and composition. When drawing diagrams, remember the compartment structure and visibility symbols. Be careful with multiplicities and arrow directions. For the IA, demonstrate a solid understanding of OOP by applying encapsulation, inheritance, and polymorphism appropriately in your own project.

掌握 UML 符号,特别是聚合与组合的区别。绘制图表时,记住分栏结构和可见性符号。注意多重性和箭头方向。对于内部评估,通过在自己的项目中恰当地应用封装、继承和多态,展示对 OOP 的扎实理解。

Key terms to revise: instantiation, constructor chaining, dynamic binding, method signature, abstract method, interface implementation, ‘is-a’ vs ‘has-a’ relationship. Create a glossary with brief definitions and examples to reinforce your memory before the exam.

需要复习的关键术语:实例化、构造函数链、动态绑定、方法签名、抽象方法、接口实现、“是一个”与“拥有”关系。制作一份包含简短定义和示例的词汇表,在考前巩固记忆。

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课程辅导,国外大学本科硕士研究生博士课程论文辅导Cancel reply

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

Exit mobile version