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

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

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

7.2.1 泛化和特化

我们来设计Dog类模型继承体系图。先创建一个名为Dog的类,我们找出了各种各样品种的狗的共同之处。该概念有时被称为泛化到特化,这是使用继承时的又一个重要的考虑因素。即在继承树中越往下,事情变得越特殊。最通用的事情位于树的顶端。在Dog继承树中,Dog类是树的顶端,并且是最通用的分类。其他品种的类(GoldenRetriever类,LhasaApso类和Basenji类)更具体。继承的思想就是通过分解共性从通用走向具体。

图7.4 Dog类继承体系图

在Dog继承模型中,我们刚开始通过找出共同的行为来建模。比如尽管金毛猎犬和拉萨阿普索犬拥有不同的行为,但这些品种的狗都有一些共同的行为,比如它们都会吠叫。然后我们意识到不是所有的狗都会吠叫,有些会用假嗓音叫。因此,我们不得不把吠叫行为放置到单独的BarkingDog类中,而将用假嗓音叫的行为放置到YodelingDog类中。然而,我们意识到吠叫的狗和不会吠叫的狗仍然共享一些通用的行为,比如它们都会喘气。因此我们保留Dog类,让BarkingDog和YodelingDog类继承自它。现在Basenji可以继承自YodelingDog,而LhasaApso和GoldenRetrierer可以继承自BarkingDog。

我们也可以决定不为BarkingDog和YodelingDog创建两个截然不同的类。我们可以为每个独立品种的类实现各自的吠叫和假嗓音叫,因为每种狗的叫声是不同的。这只是众多设计决策的其中一个示例。大概最好的方案是将吠叫和喘气作为接口实现。第8章会讨论这一点。 ...

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

桑德.范.乌格特
Go程序设计语言

Go程序设计语言

艾伦A. A.多诺万, 布莱恩W. 柯尼汉

Publisher Resources

ISBN: 9787111684527