我有以下代码,有两个类-一个点和一个圆:
import math
class Point:
"""Two-Dimensional Point(x, y)"""
def __init__(self, x=0, y=0):
# Initialize the Point instance
self.x = x
self.y = y
def __iter__(self):
yield self.x
yield self.y
def __iadd__(self, other):
self.x = self.x + other.x
self.y = self.y + other.y
return self
def __add__(self, other):
return Point(self.x + other.x, self.y + other.y)
def __mul__(self, other):
mulx = self.x * other
muly = self.y * other
return Point(mulx, muly)
def __rmul__(self, other):
mulx = self.x * other
muly = self.y * other
return Point(mulx, muly)
@classmethod
def from_tuple(cls, tup):
x, y = tup
return cls(x, y)
def loc_from_tuple(self, tup):
# self.x=t[0]
# self.y=t[1]
self.x, self.y = tup
@property
def magnitude(self):
# """Return the magnitude of vector from (0,0) to self."""
return math.sqrt(self.x ** 2 + self.y ** 2)
def distance(self, self2):
return math.sqrt((self2.x - self.x) ** 2 + (self2.y - self.y) ** 2)
def __str__(self):
return 'Point at ({}, {})'.format(self.x, self.y)
def __repr__(self):
return "Point(x={},y={})".format(self.x, self.y)
class Circle(Point):
"""Circle(center, radius) where center is a Point instance"""
def __init__(self, center= Point(0,0), radius=1):
# Point.__init__(self,center)
self.center = 1 * center
self.radius = radius
# if not isinstance(center,Point):
# raise TypeError("The center must be a Point!")
@property
def center(self):
return self._center
@center.setter
def center(self, _center):
self._center = _center
if (isinstance(self._center, Point) is False):
raise TypeError("The center must be a Point!")
def __getitem__(self,item):
return self.center[item]
def __add__(self,other):
return Circle(
Point(self.center.x + other.center.x, self.center.y+other.center.y),
self.radius + other.radius)
@classmethod
def from_tuple(cls, center,radius):
return cls(center, radius)
@property
def radius(self):
return self._radius
@radius.setter
def radius(self, radius):
if radius < 0:
raise ValueError('The radius cannot be negative')
self._radius = radius
@property
def area(self):
"""Calculate and return the area of the Circle"""
return math.pi * self.radius ** 2
@property
def diameter(self):
"""Calculate and return the diameter of the Circle"""
return self.radius * 2
def __str__(self):
return "Circle with center at ({0}, {1}) and radius {2}".format(self.center.x, self.center.y, self.radius)
def __repr__(self):
return "Circle(center=Point({0}, {1}), radius={2})".format(self.center[0],self.center[1],self.radius)
正常加法按预期工作,但+=不能。以下是预期输出。在
^{pr2}$当我尝试运行此程序时,出现以下错误:
Traceback (most recent call last): File "/Users/ayushgaur/Downloads/HW8_files (1)/shapes.py", line 206, in circle1 += circle2 File "/Users/ayushgaur/Downloads/HW8_files (1)/shapes.py", line 18, in iadd self.x = self.x + other.x AttributeError: 'Circle' object has no attribute 'x
有人知道为什么会这样吗?在
class Circle(Point):
表示类Circle
是从类Point
继承而来的,因此Point
的函数{Point
继承Circle
。我建议删除继承,即简单地使用
class Circle:
并为circle定义一个函数__iadd__
,这个函数定义了circle1 += circle2
的功能。Circle
继承了Point
的__iadd__()
方法,在调用+=
时使用。您可能希望重写
__iadd__()
方法:相关问题 更多 >
编程相关推荐