1. python是否支持多继承,请举例说明
支持多继承
class Calculator: def calculate(self, expression): return eval(expression) class MyPrint: def print(self, msg): print('msg:', msg) class MyCalculator1(Calculator, MyPrint): pass my1 = MyCalculator1() v = my1.calculate("1 + 3 * 6") my1.print(v)
2. 如果python类的多个父类存在相同的成员,按着什么规则处理
class Calculator: def calculate(self, expression): self.value = eval(expression) return self.value def printResult(self): print('计算结果:{}'.format(self.value)) class MyPrint: def print(self, msg): print('msg:', msg) def printResult(self): print('result:{}'.format(self.value)) class MyCalculator1(Calculator, MyPrint): pass class MyCalculator2(MyPrint, Calculator): pass my1 = MyCalculator1() v = my1.calculate("1 + 3 * 6") my1.print(v) my1.printResult() my2 = MyCalculator2() v = my2.calculate("1 + 3 * 6") my2.print(v) my2.printResult()
msg: 19
计算结果:19
msg: 19
result:19
如果多个父类存在冲突的成员,那么会使用最先遇到的成员(按照继承类的先后顺序)