69 - Python类是否支持多继承,请举例说明

Python是否支持多继承,请举例说明

  • Python支持多继承
class Calculator:
    def calculator(self, expression):
        self.value = eval(expression)
        return self.value
    
    def printResult(self):
        print('result: {}'.format(self.value))
        
class MyPrint:
    def print(self, msg):
        print('msg: ', msg)
        
    def printResult(self):
        print('结果: {}'.format(self.value))
        
class MyCalculator1(Calculator, MyPrint):
    pass

class MyCalculator2(MyPrint, Calculator):
    pass

my1 = MyCalculator1()
print(my1.calculator('8 + 2 * 6'))
my1.print('hello')

my2 = MyCalculator2()
print(my2.calculator('1 + 1 * 1'))
my1.print('world')
20
msg:  hello
2
msg:  world

如果Python类的多个父类存在相同的成员,按着什么规则处理

  • 如果多个分类存在冲突的成员,会使用最先遇到的成员
my1.printResult()

my2.printResult()
result: 20
结果: 2

70 - 描述异常捕捉语句中else 的作用

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章