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

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

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

第9章 创建对象及面向对象设计

前面两个章节讲述了继承和组合这两个主题。第7章中我们学到了继承和组合是创建对象的两种主要方式。而第8章中我们学习了不同程度的继承,以及继承、接口、抽象类和组合是如何在一起使用的。

本章会讨论在整体设计中对象之间是如何相互关联的。你可能认为之前已经讲过该问题,的确如此。继承和组合代表了对象交互的方式。然而继承和组合在构建对象时有一处明显的不同。当使用继承时(至少从概念上来说)最终结果是子类包括了整个继承体系中的所有行为和属性。而当使用组合时,会用一些类构建其他类。

虽然继承确实表示了两个类之间的关系,但真正的情况是创建了一个父类,它包含了子类的属性和方法。我们再来看Person(人)和Employee(雇员)类的例子(见图9.1)。

尽管这两个类是相互独立的类,但它们之间的关系并不简单——它们之间是继承关系。通常来说一名雇员是一个人。Employee对象不会向Person对象发送消息,而且Employee对象不需要Person对象的服务。这是因为Employee对象就是Person对象。

然而组合是不同的情形。组合代表了不同对象之间的交互。第8章主要讲述了继承的多样性,本章会深入了解组合的多样性,以及对象之间如何进行交互。

图9.1 继承体系关系示例

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