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

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

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

6.2.1 结构化代码

尽管程序逻辑的根本可能充满争议,我强调根本的面向对象结构是封装、继承、多态性和组合。我看过的很多书中,结构化编程的基本结构是序列、条件判断和迭代。

序列部分是给定的,因为它看来合乎逻辑。从顶部开始,然后有逻辑地运行到底部。我认为结构化编程由条件判断和迭代构成,分别称为if语句以及循环。

请看以下代码,从0开始循环10次,如果等于5则打印出一个值:

如果用面向对象的语言来编写该段代码,那么主函数中的代码是结构化的代码。这里呈现了结构化编程的全部三个基本要素:序列、条件判断和迭代。

序列部分很容易识别,因为以下是执行的第一行:

当执行完毕后,下一行会接着被执行:

依次类推。总的来说这是一个真实的自顶向下的编程方式。从第一行开始执行,然后以此类推。

这里也有一个条件判断,作为if语句的一部分:

最后,有一个循环来完成结构化三重奏:

实际上整个循环也包含了一个条件判断:

你可以只使用这三种结构就可以编写很多代码。事实上包装的概念与结构化编程很类似,只不过它用于面向对象编程中。在结构化设计中,我们将代码包装到函数(比如本例中的main方法)中,而在面向对象设计中,我们将代码包装到对象和方法中。 ...

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