How do overridden method calls from base-class methods work?

后端 未结 5 1338
我寻月下人不归
我寻月下人不归 2021-02-18 13:01

According to the docs on inheritance:

Derived classes may override methods of their base classes. Because methods have no special privileges when calling

5条回答
  •  遇见更好的自我
    2021-02-18 13:43

    class Base():
        def m1(self):
            return self.m2()
        def m2(self):
            return 'base'
    
    class Sub(Base):
        def m2(self):
            return 'sub'
    
    b = Base()
    s = Sub()
    print(b.m1(), s.m1())
    

    prints "base sub"

提交回复
热议问题