[英]getting attribute error in this python code
Hello every one, when I run this code, I get a attribute error.
大家好,当我运行这段代码时,我得到一个属性错误。 I used two diffrent ways to find the area of pyramid but the second way is not working.
我使用了两种不同的方法来找到金字塔的区域,但第二种方法不起作用。 I do not know how to solve this problem.
我不知道如何解决这个问题。 If you know how to solve this problem, please help me.
如果你知道如何解决这个问题,请帮助我。 thanks a lot!
多谢!
class Rectangle:
def __init__(self, length, width) -> None:
self.length = length
self.width = width
def area(self) -> float:
return self.length * self.width
def perimeter(self) -> float:
return 2 * self.length + 2 * self.width
class Square(Rectangle):
def __init__(self, length) -> None:
super(Square, self).__init__(length, length)
class Cube(Square):
def surface_area(self) -> float:
face_area = super(Square, self).area()
return face_area * 6
def volume(self) -> float:
face_area = super(Square, self).area()
return face_area * self.length
class Triangle:
def __init__(self, base, height) -> None:
self.base = base
self.height = height
def tri_area(self) -> float:
return 0.5 * self.base * self.height
class RightPyramid(Square, Triangle):
def __init__(self, base, slant_height) -> None:
self.base = base
self.slant_height = slant_height
super().__init__(self.base)
def area(self) -> float:
base_area = super(Square, self).area()
perimeter = super(Square, self).perimeter()
return 0.5 * perimeter * self.slant_height + base_area
def area_2(self) -> float:
base_area = super(Square, self).area
triangle_area = super(Triangle, self).tri_area()
return triangle_area * 4 + base_area
print(RightPyramid.__mro__)
pyramid = RightPyramid(3, 6)
print(pyramid.area())
print(pyramid.area_2())
I notice you are using inheritance all the way through your classes.我注意到您在整个课程中一直在使用 inheritance。
While I agree a Square is a subclass of Rectangle, a Cube in not a subclass of Squares, it is composed of Squares (hint, read up about composition vs inheritance).虽然我同意 Square 是 Rectangle 的子类,而不是 Squares 的子类中的 Cube,但它由 Squares 组成(提示,请阅读有关组合与继承的信息)。
Also since python 3, super()
does not need arguments in most cases.此外,由于 python 3,
super()
在大多数情况下不需要 arguments。
There are a couple of problems in your code:您的代码中有几个问题:
RightPyramid.__init__
does one super().__init__(self.base)
, so how should super(Triangle, self).tri_area()
access the height
? RightPyramid.__init__
做了一个super().__init__(self.base)
,那么super(Triangle, self).tri_area()
应该如何访问height
?area_2
, you're accessing super(Square, self).area
, not actually calling it.area_2
中,您正在访问super(Square, self).area
,而不是实际调用它。 This is what I can offer you for your RightPyramid
problem.这就是我可以为您提供的
RightPyramid
问题。
class Rectangle:
def __init__(self, length, width) -> None:
self.length = length
self.width = width
def area(self) -> float:
return self.length * self.width
def perimeter(self) -> float:
return 2 * self.length + 2 * self.width
class Square(Rectangle):
def __init__(self, length) -> None:
super(Square, self).__init__(length, length)
class Triangle:
def __init__(self, base, height) -> None:
self.base = base
self.height = height
def tri_area(self) -> float:
return 0.5 * self.base * self.height
class RightPyramid:
def __init__(self, base, slant_height) -> None:
self.base = Square(base)
self.side = Triangle(base, slant_height)
def area(self) -> float:
base_area = self.base.area()
perimeter = self.base.perimeter()
return 0.5 * perimeter * self.side.height + base_area
def area_2(self) -> float:
base_area = self.base.area()
triangle_area = self.side.tri_area()
return triangle_area * 4 + base_area
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.