我写了下面的代码,试图找出如何实例化主类中的子类..我想出了一些感觉不正确的东西..至少对我而言。Python实例化子类
这种类型的实例有什么问题吗?有没有更好的方法来调用子类?
class Family():
def __init__(self):
self.Father = self.Father(self)
self.Mother = self.Mother(self)
class Father():
def __init__(self, instance = ''):
self = instance if instance != '' else self
print self
def method(self):
print "Father Method"
def fatherMethod(self):
print "Father Method"
class Mother():
def __init__(self, instance = ''):
self = instance if instance != '' else self
print self
def method(self):
print "Mother Method"
def motherMethod(self):
print "Mother Method"
if __name__ == "__main__":
Family = Family()
Family.Father.method()
Family.Mother.method()
这看起来很奇怪。为什么你将自我传递给内部类构造函数?请解释你想用这个做什么。 – 2010-01-07 10:10:12