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 参数问题