【发布时间】:2021-02-12 07:49:09
【问题描述】:
我有一个基类,如下所示:
class coordinates(object):
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
@property
def x(self):
return self.x
@x.setter
def x(self, x):
self.x = x
@property
def y(self):
return self.y
@y.setter
def y(self, y):
self.y = y
@property
def z(self):
return self.z
@z.setter
def z(self, z):
self.z = z
然后我创建一个继承自坐标的子类,并在其中包含一个静态方法,该方法将使用实例属性,如 x、y 和 z ...,如下所示:
class volume(coordinates):
def __init__(self, x, y, z):
super().__init__(x, y, z)
self.volume = self.calculate_volume()
def calculate_volume(self):
return self.x * self.y * self.z
@staticmethod
def goes_through(x, y, z, h, l):
if x < l and y < h:
return f"Use surface {x}{y} to go through"
elif y < l and x < h:
return f"Use surface {y}{x} to go through"
elif x < l and z < h:
return f"Use surface {x}{z} to go through"
elif z < l and x < h:
return f"Use surface {z}{x} to go through"
elif z < l and y < h:
return f"Use surface {z}{y} to go through"
elif y < l and z < h:
return f"Use surface {y}{z} to go through"
else:
return "Object can't go through"
然后我实例化一个对象并尝试获取它的体积并查看它是否通过以及如何通过:
obj1 = volume(100, 200, 400)
print(obj1.volume)
print(obj1.goes_through(obj1.x, obj1.y, obj1.z, 200, 350))
但是我得到了这个错误:
[上一行重复了 993 次以上] RecursionError: 最大值 超出递归深度
非常感谢任何帮助。
【问题讨论】:
-
属性定义错误。请尝试正确的定义。它会起作用的。 python-reference.readthedocs.io/en/latest/docs/functions/…
标签: python oop descriptor