所以我试图在Python 2.7中按照以下方式做一些事情:
class Print(object)
def __init__(self):
self.printer = 'something'
def pretty_print(self, text):
print('Pretty {}'.format(text))
class Report(object)
def __init__(self):
self.var1 = 1
self.var2 = 2
def return_sum(self):
return self.var1 + self.divide_by_two(self.var1 + self.var2)
class MasterClass(Report, Print):
def __init__(self):
Report.__init__(self)
Print.__init__(self)
self.execute()
def execute(self):
sum = self.return_sum()
def divide_by_two(self, number):
return number / 2
所以这里的困境就是这样我希望Report类和Print类能够从MasterClass访问方法和变量。我能以某种方式这样做吗?
最后,在这种情况下工作的解决方案是精心规划的继承和类初始化:
class Print(object)
class Report(Print)
class MasterClass(Report)