我有一个教师试图在Python中教OOP的任务。我对C++和C#中的OOP非常熟悉,但难以弄清楚我的某些code中发生了什么。它的工作原理和两个类的功能,因为我想,但我不得不添加一些奇怪的代码,它的工作,我不明白为什么。Python多态性
特别引用在行开始的代码64
class Cone(Cylinder):
#Constructor
def __init__ (self, radius, height):
Cylinder.__init__(self, radius, height)
self.calcVolume()
def calcVolume(self):
Cylinder.calcVolume(self)
self.__volume = Cylinder.GetVolume(self) * (1.0/3.0)
所以实施Cone
时,我不明白为什么当锥的构造函数调用缸的构造函数中Cylinder.calcVolume()
不叫。如果代码被隐式调用,代码对我来说会更有意义,但我不得不明确地调用该方法。一些指导或解释会很棒。
发布后我做了这个改变是否更有意义?
class Cone(Cylinder):
#Constructor
def __init__ (self, radius, height):
Cylinder.__init__(self, radius, height)
self.calcVolume()
def calcVolume(self):
self.__volume = self.GetBase().GetArea() * self.GetHeight() * (1.0/3.0)
我返工超()的代码,似乎这是需要新的类中的方法,但是这让我成左右一些Python OOP的东西多一点研究。谢谢 – Thomas