python super 理解(四)
生活随笔
收集整理的这篇文章主要介绍了
python super 理解(四)
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
原文链接
super()单继承可以为做什么呢?
像其他面向对象的语言一样,它允许您在子类中调用超类的方法。这种方法的主要用例是扩展继承方法的功能。
#长方形定义 class Rectangle:def __init__(self, length, width):self.length = lengthself.width = widthdef area(self):return self.length * self.widthdef perimeter(self):return 2 * self.length + 2 * self.width# Here we declare that the Square class inherits from the Rectangle class #正方形定义 class Square(Rectangle):def __init__(self, length):super().__init__(length, length)square = Square(4)print(square.area())#16 class Cube(Square):def __init__(self,length):super().__init__(length)def surface_area(self):face_area = super(Square, self).area()return face_area * 6def volume(self):face_area = super(Square, self).area()return face_area * self.lengthcube = Cube(3) print(cube.surface_area()) print(cube.volume( 54 27总结
以上是生活随笔为你收集整理的python super 理解(四)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: embedding 层的详细解释
- 下一篇: python super 参数问题