返回   华枫论坛 > ◆ 工作学习◆ > IT交流



发表新主题 回复
 
只看楼主 主题工具
旧 May 11th, 2012, 00:20     #1
Georgeyu100
Senior Member
级别:8 | 在线时长:96小时 | 升级还需:21小时级别:8 | 在线时长:96小时 | 升级还需:21小时级别:8 | 在线时长:96小时 | 升级还需:21小时级别:8 | 在线时长:96小时 | 升级还需:21小时
 
注册日期: Jul 2004
帖子: 174
Georgeyu100 is an unknown quantity at this point
Question problem on encapsulation of OO

One of the biggest benefits of encapsulation is that it allows programmers to change their minds about one part of a program without having to rewrite other parts. For example, suppose we want to represent rectangular sections of images.
Our first attempt might store the XY coordinates of the rectangle’s bottom-left and upper-right corners, like this:

class Rectangle(object):
'''Represent a rectangular section of an image.'''
def __init__(self, x0, y0, x1, y1):
'' 'Create a rectangle with non-zero area. (x0,y0) is the
lower left corner, (x1,y1) the upper right corner.'' '
self.x0 = x0
self.y0 = y0
self.x1 = x1
self.y1 = y1

def area(self):
'''Return the area of the rectangle.'''
return (self.x1 - self.x0) * (self.y1 - self.y0)

def contains(self, x, y):
'' 'Return True is (x,y) point is inside a rectangle,
and False otherwise.'' '
return (self.x0 <= x <= self.x1) and \
(self.y0 <= y <= self.y1)

Later, we might decide that it would be better to store the rectangle’s
lower-left corner and XY size, like this:

class Rectangle(object):
'''Represent a rectangular section of an image.'''
def __init__(self, x0, y0, width, height):
'' 'Create a rectangle with non-zero area. (x0,y0) is the
lower left corner, width and height the X and Y extent.'' '
self.x0 = x0
self.y0 = y0
self.width = width
self.height = height

def area(self):
'''Return the area of the rectangle.'''
return self.width * self.height

def contains(self, x, y):
'' 'Return True if (x,y) point is inside a rectangle,
and False otherwise.'' '
return (self.x0 <= x) and (x <= self.x0 + width) and \
(self.y0 <= y) and (y <= self.y0 + height)

If we made this change, we would obviously also have to change every piece of software that created new rectangles. However, we wouldn’t have to change code that used the area or contains methods—since they hide the details of how they calculate their results, we can change their operation without affecting anything else.

在后面的解释中:” we wouldn’t have to change code that used the area or contains methods—since they hide the details of how they calculate their results, we can change their operation without affecting anything else.”

Area() 和Rectangle() 不是都改了吗?

return (self.x0 <= x <= self.x1) and \
(self.y0 <= y <= self.y1)
变成了:
return self.width * self.height


return (self.x0 <= x <= self.x1) and \
(self.y0 <= y <= self.y1)
变成了:
return (self.x0 <= x) and (x <= self.x0 + width) and \
(self.y0 <= y) and (y <= self.y0 + height)

怎么说:” we wouldn’t have to change code”? 不懂.

后面, 它又说:” we can change their operation without affecting anything else.”
难道说上面的改变只是它的操作的改变, 不是代码的改变? 那如果这个例子写成IF语句, 而不是返回语句, 放在函数里面, 代码不是改变了?
Georgeyu100 当前离线  
回复时引用此帖
旧 May 11th, 2012, 01:05   只看该作者   #2
somedy
不堪盈手赠 还寝梦佳期
级别:31 | 在线时长:1120小时 | 升级还需:32小时级别:31 | 在线时长:1120小时 | 升级还需:32小时级别:31 | 在线时长:1120小时 | 升级还需:32小时
 
somedy 的头像
 
注册日期: Jul 2004
帖子: 864
声望: 794908
somedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond repute
默认

至少把句子读全吧... 人家说we wouldn’t have to change code that used the area or contains methods, 你怎么把后半段砍了呢?使用area()和contains()的代码文中没有出现。看来你读英文书太粗,看中文书又太细,"不求甚解"是门学问,需要好好体会。

『 吾今落魄邯郸道 欲向先生借枕头 』
somedy 当前离线  
回复时引用此帖
旧 May 11th, 2012, 10:57   只看该作者   #3
Georgeyu100
Senior Member
级别:8 | 在线时长:96小时 | 升级还需:21小时级别:8 | 在线时长:96小时 | 升级还需:21小时级别:8 | 在线时长:96小时 | 升级还需:21小时级别:8 | 在线时长:96小时 | 升级还需:21小时
 
注册日期: Jul 2004
帖子: 174
声望: 0
Georgeyu100 is an unknown quantity at this point
默认

引用:
作者: somedy 查看帖子
至少把句子读全吧... 人家说we wouldn’t have to change code that used the area or contains methods, 你怎么把后半段砍了呢?
我是想引用到那里就够了, 能表达我的不解了, 不必要更多来浪费大家的时间.
Georgeyu100 当前离线  
回复时引用此帖
旧 May 11th, 2012, 11:07   只看该作者   #4
Georgeyu100
Senior Member
级别:8 | 在线时长:96小时 | 升级还需:21小时级别:8 | 在线时长:96小时 | 升级还需:21小时级别:8 | 在线时长:96小时 | 升级还需:21小时级别:8 | 在线时长:96小时 | 升级还需:21小时
 
注册日期: Jul 2004
帖子: 174
声望: 0
Georgeyu100 is an unknown quantity at this point
默认

引用:
作者: somedy 查看帖子
使用area()和contains()的代码文中没有出现。
正是因为如此, 感觉这个例子不懂, 因为把RETURN语句中的代码写到里面, 结果就全变了(代码必须改了). 当然, 这样的RETURN语句更好, 提出"代码写到里面"是设想这个问题的多种可能性.

请指教.
Georgeyu100 当前离线  
回复时引用此帖
旧 May 11th, 2012, 12:43   只看该作者   #5
somedy
不堪盈手赠 还寝梦佳期
级别:31 | 在线时长:1120小时 | 升级还需:32小时级别:31 | 在线时长:1120小时 | 升级还需:32小时级别:31 | 在线时长:1120小时 | 升级还需:32小时
 
somedy 的头像
 
注册日期: Jul 2004
帖子: 864
声望: 794908
somedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond repute
默认

引用:
作者: Georgeyu100 查看帖子
正是因为如此, 感觉这个例子不懂, 因为把RETURN语句中的代码写到里面, 结果就全变了(代码必须改了). 当然, 这样的RETURN语句更好, 提出"代码写到里面"是设想这个问题的多种可能性.

请指教.
指教不敢当,我也是写程序的,不是语文老师,说错了你包涵一二。你引用的这段话的意思是因为用了封装,虽然Area()和Contains()的实现(implementation)改变了,调用它们的代码不用改。给个例子好了,假设obj引用(reference)了一个Rectangle的实例(instance),下面的代码就无需跟着Area()或者Contains()修改,

代码:
if ( obj->Contains(x, y) == true )
{
    return  obj->Area();
}
else
{
    return -1;
}
somedy 当前离线  
回复时引用此帖
旧 May 11th, 2012, 13:17   只看该作者   #6
Georgeyu100
Senior Member
级别:8 | 在线时长:96小时 | 升级还需:21小时级别:8 | 在线时长:96小时 | 升级还需:21小时级别:8 | 在线时长:96小时 | 升级还需:21小时级别:8 | 在线时长:96小时 | 升级还需:21小时
 
注册日期: Jul 2004
帖子: 174
声望: 0
Georgeyu100 is an unknown quantity at this point
默认

引用:
作者: somedy 查看帖子
虽然Area()和Contains()的实现(implementation)改变了,调用它们的代码不用改。
是不是我可以这样理解:
"Area()和Contains()的实现(implementation)改变了" 是指 RETURN语句中的代码改变了.

然后, 因为类是封装的, RETURN语句中的代码改变, 外面看不见. 所以, 调用代码也不用改变. 就是说, 你给的例子可照旧用, 对吗?
Georgeyu100 当前离线  
回复时引用此帖
旧 May 11th, 2012, 13:24   只看该作者   #7
somedy
不堪盈手赠 还寝梦佳期
级别:31 | 在线时长:1120小时 | 升级还需:32小时级别:31 | 在线时长:1120小时 | 升级还需:32小时级别:31 | 在线时长:1120小时 | 升级还需:32小时
 
somedy 的头像
 
注册日期: Jul 2004
帖子: 864
声望: 794908
somedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond repute
默认

引用:
作者: Georgeyu100 查看帖子
是不是我可以这样理解:
"Area()和Contains()的实现(implementation)改变了" 是指 RETURN语句中的代码改变了.

然后, 因为类是封装的, RETURN语句中的代码改变, 外面看不见. 所以, 调用代码也不用改变. 就是说, 你给的例子可照旧用, 对吗?...
对。
你真的叫Georgey吗?No offense,感觉你的思维方式很女性化...
somedy 当前离线  
回复时引用此帖
旧 May 11th, 2012, 14:15   只看该作者   #8
Georgeyu100
Senior Member
级别:8 | 在线时长:96小时 | 升级还需:21小时级别:8 | 在线时长:96小时 | 升级还需:21小时级别:8 | 在线时长:96小时 | 升级还需:21小时级别:8 | 在线时长:96小时 | 升级还需:21小时
 
注册日期: Jul 2004
帖子: 174
声望: 0
Georgeyu100 is an unknown quantity at this point
默认

Thank you for your help. The example I referenced above looks not very good. It does't explain clearly what it wants to say. It should include one more part like you gave.

我向来认: 学习应真正理解, 不能只是一知半解.
Georgeyu100 当前离线  
回复时引用此帖
旧 May 11th, 2012, 14:24   只看该作者   #9
somedy
不堪盈手赠 还寝梦佳期
级别:31 | 在线时长:1120小时 | 升级还需:32小时级别:31 | 在线时长:1120小时 | 升级还需:32小时级别:31 | 在线时长:1120小时 | 升级还需:32小时
 
somedy 的头像
 
注册日期: Jul 2004
帖子: 864
声望: 794908
somedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond reputesomedy has a reputation beyond repute
默认

引用:
作者: Georgeyu100 查看帖子
我向来认: 学习应真正理解, 不能只是一知半解.
Sounds like my 3rd grade teacher... again, no offense... I used to believe her...
somedy 当前离线  
回复时引用此帖
发表新主题 回复


发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛启用 表情符号
论坛启用 [IMG] 代码
论坛禁用 HTML 代码



所有时间均为格林尼治时间 -4。现在的时间是 13:39

请尊重文章原创者,转帖请注明来源及原作者。
凡是本站用户自行发布的任何信息,皆不代表本站的立场,
华枫网站不确保各类信息的正确性和可靠性,也不承担由此而导致的任何直接或间接损失以及任何法律责任。

Copyright © 1999-2024 Chinasmile