我對學校的任務有問題。我想要我的最後一種方法來測試兩個矩形是否相同。唯一的問題是,我似乎無法區分兩個不同矩形的兩個不同高度,widiths和不同點(這是矩形的左下角點),有什麼建議嗎?Python:如何區分同一類中兩個不同對象的兩個變量?
非常感謝
class Point():
def __init__(self,x,y):
self.x=x
self.y=y
class Rectangle():
def __init__(self,Point,w,h):
self.Point=Point
self.widith=w**strong text**
self.height=h
def same(self,Rectangle):
if Rectangle.self.Point==self.Point and Rectangle.self.widith==self.widith and Rectangle.self.height==self.height:
return True
else:
return False
我不會使用'Point'參數'(__init __(self,Point ...)'和'class Point'(我會做'__init __(self,point, w,h)')然後我會看看該類的'__eq__'方法(https://docs.python.org/2/reference/datamodel.html#object.__eq__)。如果你在'Point'和'Rectangle'類中覆蓋它,你可以使用'rectangle1 == rectangle2' – BorrajaX