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

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

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

1.5.2 对象行为

对象的行为表示对象可以做什么。在过程式语言中,可以通过过程、函数及子程序定义行为。在面向对象程序设计术语中,这些行为包含在方法中,你可以通过发送消息的方式来调用方法。在雇员(employee)例子中,考虑到employee对象需要的行为之一是对属性值进行存取,所以每个属性会有相应的方法,比如setGender()和getGender()。当另一个对象需要该信息时,可以给employee对象发送一个消息来询问其性别。

毫不奇怪,在本书第1版发布时,应用程序中已经出现了取值方法(getter)和赋值方法(setter),当然还有很多其他的面向对象技术。而且很多技术都与数据相关。要记住,使用对象最有趣更不用说强大的一点在于数据是包的一部分,而不是与行为代码割裂。

XML的出现并不全为了以可移植的方式表示数据。它也有一种替代方式来让代码轻松访问数据。在.NET技术中,取值方法和赋值方法都被认为是数据自身的属性。

例如,Java中定义一个名为Name的属性如下所示:

public String Name;

对应的取值方法和赋值方法如下所示:

public void setName (String n) {name = n;};
public String getName() {return name;};

现在,当创建一个名为Name的XML属性时,C#.NET中的定义可能是这样的,尽管你也可以使用与Java示例相同的方法:

private string strName; public String Name { get { return this.strName; } set { if (value == null) return; this.strName = value; ...
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