python 问题 关于类的

classPoint:"""Aclasstorepresentatwo-dimensionalpoint"""def__init__(self):self.x=0self... class Point:
"""A class to represent a two-dimensional point"""
def __init__(self):
self.x = 0
self.y = 0

class Rectangle:
def __init__(self):
self.corner = Point()
self.width = 100
self.height = 100

def equals(self, otherRect):
"""
Test if this rectangle is equal to otherRect. Deep equality should be tested.
>>> r1 = Rectangle()
>>> r1.corner.x = 50
>>> r1.corner.y = 40
>>> r1.width = 200
>>> r2 = Rectangle()
>>> r2.corner.x = 50
>>> r2.corner.y = 40
>>> r2.width = 200
>>> r1.equals(r2)
True
>>> r2.height = 200
>>> r1.equals(r2)
False
"""
#add an equals(self, otherRect) method to the Rectangle class. the method should do a deep quality check. Recall that deep equality means that equals should return True if the contents of the structure are equal and False otherwise. here above is the skeleton and two doctests for the method.
#就是说在上面的equals 里添加语句 让上面的测试通过。 我实在不会做 希望大家能帮我这个忙,谢谢了!
展开
 我来答
tim_spac
2011-05-10 · TA获得超过3629个赞
知道大有可为答主
回答量:1804
采纳率:100%
帮助的人:2074万
展开全部
class Point:
"""A class to represent a two-dimensional point"""
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __eq__(self,other):
return self.x == other.x and self.y == other.y

class Rectangle:
def __init__(self, corner=Point(), width=100, height=100):
self.corner = corner
self.width = width
self.height = height

def equals(self, otherRect):
return self.corner == otherRect.corner \
and self.width == otherRect.width \
and self.height == otherRect.height

def __eq__(self, otherRect):
return self.equals(otherRect)
"""
Test if this rectangle is equal to otherRect. Deep equality should be tested.
>>> r1 = Rectangle()
>>> r1.corner.x = 50
>>> r1.corner.y = 40
>>> r1.width = 200
>>> r2 = Rectangle()
>>> r2.corner.x = 50
>>> r2.corner.y = 40
>>> r2.width = 200
>>> r1.equals(r2)
True
>>> r2.height = 200
>>> r1.equals(r2)
False
"""
追问
为什么不能写成
if self.corner == otherRect.corner and self.width == otherRect.width:
return True
else:
return self.height == otherRect.height???
追答
相等的逻辑应该是几个数据属性完全一样: corner一样, width一样, 且 height也一样;
任意一个属性有差异就是不同
shunh9hf
2011-05-08 · TA获得超过216个赞
知道小有建树答主
回答量:390
采纳率:0%
帮助的人:266万
展开全部
return self.width == otherRect.width and self.height == otherRect.height and self.corner.x == otherRect.corner.x and self.corner.y == otherRect.corner.y
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式