Skip to Content
面向对象的思考过程(原书第5版)
book

面向对象的思考过程(原书第5版)

by 马特 魏斯费尔德
August 2021
Intermediate to advanced
450 pages
4h 46m
Chinese
Pearson
Content preview from 面向对象的思考过程(原书第5版)

9.6 一个综合性示例

我们来通过一个简单的例子来把继承、接口、组合、联合和聚合概念糅合到一个简单、短小的系统中。

我们使用第8章中的例子,并做了以下扩展:我们添加一个Owner类用来遛狗。

Dog类直接从Mammal类继承。图9.9中的实体箭头表示了Dog类和Mammal类之间的关系。Nameable类是Dog实现的接口,通过Dog类和Nameable接口之间的虚线箭头表示这种关系。

图9.9 Dog例子中的UML类图

在本章中,我们一直在讨论联合和聚合。Dog类和Head类之间是聚合关系,因为头实际上是狗的一部分。连接这两个类图的线上的基数表示了一条狗只能有一个头。

Dog类和Owner之间是联合的关系。主人当然不是狗的一部分,反过来也一样,所以肯定不是聚合关系。然而狗需要主人的服务(遛狗)。Dog和Owner类之间的线基数指定狗可以拥有一个或多个主人(例如,妻子和丈夫都可以认为是狗的主人,都有遛狗的责任)。

当设计面向对象的系统时,主要就是使用继承、接口、组合、联合和聚合等手段组织对象之间的关系。

Head类在哪里?

你可能认为将Head类附加到Mammal类上而不是Dog类上更合理,因为所有的哺乳动物一般都只有一个头。在该模型中我只是想突出Dog类,所以才会把Head类附加到Dog类上。

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Node与Express开发(第2版)

Node与Express开发(第2版)

Ethan Brown
手把手教会你linux

手把手教会你linux

桑德.范.乌格特
Java并发编程实战

Java并发编程实战

Brian Goetz, Tim Peierls

Publisher Resources

ISBN: 9787111684527