First exams 2027 · SL & HL2027 年首次开考 · SL 与 HL

B3.1.2 · Fundamentals of OOP for a single class

B3.1.2 · 单个类的 OOP 基础

Designing classes, and drawing them with UML

设计类,并用 UML 画出来

Before any code gets written: turning a plain description of a system into the classes, methods, and relationships it actually needs.

在写任何代码之前:把对一个系统的普通文字描述,变成它真正需要的类、方法和关系。

Core for SL & HL Typically 4–6 marks · AO2/AO3 Command term: construct
SL 与 HL 核心内容 通常 4–6 分 · AO2/AO3 指令词:construct

Explainer

讲解

Meridian Eats, a food delivery startup, hands you one paragraph and asks for a backend: "A customer places an order. Each order is made up of one or more items chosen from the menu. Once an order is ready, a courier delivers it to the customer and marks it as delivered." No classes, no code, just a description. Before writing a single line, that description has to become an actual design, and Unified Modelling Language (UML) is the standard way to draw it.

外卖创业公司 Meridian Eats 给了你一段话,请你做一个后端:"顾客下一个订单。每个订单由从菜单中选出的一个或多个菜品组成。订单准备好后,由骑手把它送到顾客手中,并标记为已送达。"没有类,没有代码,只有一段描述。在写下任何一行代码之前,这段描述必须变成一个真正的设计,而统一建模语言(UML)就是画出它的标准方式。

Df

A UML class diagram is a standard diagrammatic notation for showing a system's classes, each one's attributes and methods, and how the classes relate to one another, so the design can be planned and checked before any code is written.

UML 类图是一种标准的图示符号,用来展示系统的类、每个类的属性和方法,以及类之间如何相互关联,这样就能在写任何代码之前先规划并检查设计。

!

This statement asks for two separate skills, both tested: (1) deriving which classes, attributes, and methods a system actually needs from its written requirements, and (2) representing that design correctly using UML's own notation, boxes, visibility symbols, relationship lines, and multiplicity. A good design badly drawn, or a correct-looking diagram for the wrong design, both lose marks.

这条陈述要求两项互不相同的技能,而且都会考:(1) 推导出一个系统根据书面需求实际需要哪些类、属性和方法;(2) 表示出这个设计,即用 UML 自己的符号正确地画出来,包括方框、可见性符号、关系线和多重性。一个好的设计画得不对,或者一张看起来正确的图却对应错误的设计,都会丢分。

The visibility symbols inside each box (+ public, − private) were introduced when B3.1.5 covered encapsulation. This page's own focus is what sits between the boxes: how classes connect to each other, and what that connection is allowed to mean.

每个方框里的可见性符号(+ 表示公有,− 表示私有)在 B3.1.5 讲封装时已经介绍过。本页的重点是方框之间的部分:类如何相互连接,以及这种连接能表示什么意思。

Explainer

讲解

Three relationship types, one question each

三种关系类型,各对应一个问题

A line between two classes in a UML diagram is never just "these are related." It always answers one specific question: if the whole is deleted, does the part still make sense on its own?

UML 图中两个类之间的一条线,从来不只是"这两者有关系"。它总是在回答一个具体的问题:如果"整体"被删除了,"部分"自己还说得通吗?

Association

关联

Two classes are linked, but each one exists completely independently of the other.

两个类之间有联系,但各自完全独立地存在。

Later in this diagram: Order and Courier. An order can exist before any courier is assigned, and a courier keeps existing long after any one delivery finishes.

本图稍后会出现:Order 和 Courier。订单可以在分配任何骑手之前就存在,而骑手在任何一次配送结束之后仍会继续存在。

Aggregation

聚合

A "whole and part" link where the part can still exist on its own, and could even belong to a different whole instead.

一种"整体与部分"的联系,其中部分可以单独存在,甚至可以转而属于另一个整体。

A separate example, not in this diagram: a Restaurant has MenuItems, but a MenuItem's recipe still means something even if that restaurant closes, and the same dish could be listed by another restaurant entirely.

另一个例子,不在本图中:一家 Restaurant 有若干 MenuItem,但即使这家餐厅关门了,菜品的配方仍然有意义,而且同一道菜完全可以被另一家餐厅列出。

Composition

组合

A stricter "whole and part" link. The part has no independent life of its own: it is created for one particular whole, and it stops being meaningful the moment that whole is gone.

一种更严格的"整体与部分"的联系。部分没有自己独立的生命:它是为某一个特定的整体而创建的,一旦那个整体消失,它就失去意义。

Later in this diagram: Order and OrderItem. An OrderItem only ever means "one line of this specific order." Cancel the order, and its items are meaningless without it.

本图稍后会出现:Order 和 OrderItem。一个 OrderItem 只表示"这一个特定订单中的一行"。订单取消后,它的各个项目就没有意义了。

Explainer

讲解

Reading multiplicity: the numbers at each end of the line

读懂多重性:线两端的数字

Knowing two classes are associated still leaves the most useful question unanswered: how many? A number sits at each end of every relationship line, and it is not decoration, it is part of the design.

知道两个类有关联,仍然没有回答最有用的问题:有多少个?每条关系线的两端各有一个数字,它不是装饰,而是设计的一部分。

Df

Multiplicity states how many instances of one class can take part in a relationship with a single instance of the class at the other end. It is written as a minimum and a maximum separated by two dots, and * means "no upper limit."

多重性(multiplicity)说明一个类的多少个实例,可以与线另一端那个类的单个实例发生关系。它写成最小值和最大值,中间用两个点隔开,* 表示"没有上限"。

Notation符号Minimum最小值Maximum最大值Reads as读作
111exactly one, never zero and never more恰好一个,不会是零个,也不会更多
0..101optional: either none or a single one可选:没有,或者恰好一个
1..*1no limit无上限at least one, possibly many至少一个,可能很多
0..*0no limit无上限any number at all, including none任意数量,包括没有

* on its own is a common shorthand for 0..*, and both are accepted.

单独一个 * 是 0..* 的常见简写,两种写法都被接受。

!

Which end does a number describe? Read it by starting at the class on the opposite side. The number sitting next to a class tells you how many of that class go with one instance of the class across the line.

一个数字描述的是哪一端?读的时候,从另一侧的类开始。紧挨着某个类的数字,告诉你对线另一端那个类的一个实例而言,有多少个这个类的实例与之对应。

So in Customer 1 —— 0..* Order, the 0..* next to Order means one Customer can have any number of Orders, including none yet. Reading it back the other way, the 1 next to Customer means each Order belongs to exactly one Customer.

所以在 Customer 1 — 0..* Order 中,Order 旁边的 0..* 表示一个 Customer 可以有任意数量的 Order,包括暂时还没有。反过来读,Customer 旁边的 1 表示每个 Order 恰好属于一个 Customer。

Every multiplicity choice is a real design decision that a later question can ask you to justify. Writing 1..* between Order and OrderItem, rather than 0..*, is a deliberate claim: an order with zero items is not a valid order at all.

每一个多重性的选择都是真实的设计决定,之后的题目可能会要求你说明理由。在 Order 和 OrderItem 之间写 1..* 而不是 0..*,是一个有意的主张:没有任何项目的订单根本不是有效订单。

Worked example

例题讲解

From requirements to classes: Meridian Eats

从需求到类:Meridian Eats

The same requirements paragraph from the top of the page, turned into a design one deliberate step at a time.

还是页面开头的那段需求,一步一步、有意识地变成一个设计。

Constructing the design Step 1 of 4
构建设计 第 1 步,共 4 步

Step 1. Read the requirements, and mark the candidate nouns and verbs. Nouns tend to become classes. Verbs tend to become methods.

第 1 步。阅读需求,标出候选的名词和动词。名词往往会成为类,动词往往会成为方法。

A customer places an order. Each order is made up of one or more items chosen from the menu. Once an order is ready, a courier delivers it to the customer and marks it as delivered.
一位顾客下一个订单。每个订单由从菜单中选出的一个或多个菜品组成。订单准备好后,由一位骑手把它送到顾客手中,并标记为已送达。

candidate class candidate method

候选类 候选方法

Step 2. List the candidate classes, with just their attributes. No methods yet, just what data each class needs to hold.

第 2 步。列出候选类,只写属性。先不写方法,只写每个类需要保存哪些数据。

Customer
Attribute属性
name
email
Order
Attribute属性
order_id
status
OrderItem
Attribute属性
name
price
quantity
Courier
Attribute属性
name
vehicle_type

Step 3. Add the methods. Each verb from Step 1 becomes a method on whichever class is responsible for that behaviour.

第 3 步。添加方法。第 1 步中的每个动词,都变成负责该行为的那个类上的一个方法。

Customer
Member成员
name
email
+ place_order()
Order
Member成员
order_id
status
+ add_item()
+ calculate_total()
OrderItem
Member成员
name
price
quantity
Courier
Member成员
name
vehicle_type
+ deliver_order()

Notice OrderItem got no new methods. Not every class needs behaviour of its own, some exist purely to hold structured data.

注意 OrderItem 没有新增方法。并不是每个类都需要自己的行为,有些类纯粹是用来保存结构化数据的。

Step 4. Identify how the classes relate, and what type each relationship is. This uses the same three types just covered above.

第 4 步。确定类之间如何关联,以及每个关系属于哪种类型。这里用的就是上面刚讲过的那三种类型。

Classes类Type类型Multiplicity多重性Meaning含义
Customer – OrderAssociation关联1 to 0..*places下单
Order – OrderItemComposition组合1 to 1..*made up of由……组成
Order – CourierAssociation关联0..1 to 0..1delivered by由……配送

Worked example

例题讲解

The assembled diagram

组装好的图

Everything from the stepper above, drawn as one real UML class diagram. Click any class to see exactly how it connects.

上面步进器里的所有内容,画成一张完整的 UML 类图。点击任一个类,可以看到它具体如何与其他类连接。

Click a class box to highlight its relationships.

点击一个类的方框,高亮它的各个关系。

1 0..* 1 1..* 0..1 0..1
Customer
− name : string
− email : string
+ place_order()
Order
− order_id : string
− status : string
+ add_item()
+ calculate_total()
OrderItem
− name : string
− price : real
− quantity : int
Courier
− name : string
− vehicle : string
+ deliver_order()

 

Check your understanding

检查你的理解

An OrderItem only ever means "one line of this specific order," and is deleted the moment its order is cancelled. Which relationship type should connect Order and OrderItem?

一个 OrderItem 只表示"这一个特定订单中的一行",订单一取消它就被删除。Order 和 OrderItem 之间应该用哪种关系类型连接?

Common mistake

常见错误

Markers reported this pattern for B3.1.2
阅卷员反馈了 B3.1.2 的这一常见模式

RecurringCandidates frequently use aggregation and composition interchangeably, drawing whichever diamond they remember more easily rather than checking which one actually matches the existence-dependency described in the scenario.

反复出现考生经常把聚合和组合混用,谁的菱形更好记就画哪个,而不去核对到底哪一个符合场景中描述的存在依赖关系。

Answer frame that scores: state whether the part can exist without the whole → name the matching relationship type → justify it using that existence rule specifically, not just "they are related."

能得分的答题框架:说明部分能否脱离整体而存在 → 指出与之对应的关系类型 → 用这条存在规则具体地给出理由,而不是只说"它们有关联"。

✕

What a weak answer looks like: “Order and OrderItem are composed together because they are related.” This never states the actual rule that separates composition from aggregation, so it would score the same even if the wrong diamond were drawn.

较弱的答案是这样的:“Order 和 OrderItem 是组合在一起的,因为它们有关联。”这没有说出区分组合与聚合的那条真正的规则,所以即使画错了菱形,得分也是一样的。

✓

What it should say: “Order and OrderItem use composition, not aggregation, because an OrderItem cannot exist without its specific Order: deleting the Order deletes its OrderItems too. This differs from Restaurant and MenuItem, where a MenuItem can still exist even if that Restaurant closes.” Names the rule, applies it, and contrasts it against the other type.

应该这样写:“Order 和 OrderItem 使用组合而不是聚合,因为 OrderItem 不能脱离它所属的那个 Order 而存在:删除 Order 也会删除它的 OrderItem。这不同于 Restaurant 和 MenuItem,后者即使那家 Restaurant 关门了,MenuItem 仍然可以存在。”说出了规则,运用了规则,并与另一种类型作了对比。

Linking question

联系性问题

How this connects to the rest of the syllabus

这与考纲其余部分如何联系

A3

In what ways can OOP be applied to database development? (A UML class diagram and a database schema describe almost the same shape from two different angles: a class's attributes become a table's fields, an association with multiplicity becomes a foreign key relationship, and composition, where the part cannot outlive the whole, becomes a cascading delete.)

OOP 如何应用于数据库开发?(UML 类图和数据库模式从两个不同的角度描述几乎相同的形状:类的属性变成表的字段,带多重性的关联变成外键关系,而组合,即部分不能比整体存活得更久,则变成级联删除。)

Revision questions

复习题

Past-paper style. Write an answer, then reveal the marking points and mark yourself.

仿真题风格。先写出你的答案,再显示评分要点并给自己打分。

Question 1 · 2 marks第 1 题 · 2 分

[2]Distinguish between aggregation and composition, using an example of each.

[2]区分聚合与组合,并各举一个例子。

Question 2 · 3 marks第 2 题 · 3 分

[3]A requirements document says: "A gym member can book multiple classes. Each class has one instructor." Identify the candidate classes and the relationship between them.

[3]一份需求文档写道:"一名健身房会员可以预约多个课程。每个课程有一位教练。"找出候选类以及它们之间的关系。

Question 3 · 4 marks第 3 题 · 4 分

[4]Construct a UML class diagram fragment showing Order and OrderItem, including multiplicity and the correct relationship notation.

[4]构建一个 UML 类图片段,展示 Order 和 OrderItem,要包括多重性和正确的关系符号。

Flashcards

闪卡

Active recall for this statement’s key terms.

针对本条陈述关键术语的主动回忆。

Prototype render · content grounded in real IB CS 2027 marker-report data for B3.1.2 · not yet wired to live routes