python继承

python继承

class parent(object):
    def __init__(self):
        super(parent, self).__init__()
        self.func = None
    def func_hand(self):
        self.func()

def printf():
    print('hello python!')

class child(parent):
    def __init__(self):
        super(child, self).__init__()
        self.func = printf
    def test(self):
        self.func_hand()

instance = child()
instance.test()

父类中self.func并没有定义,在子类中定义,然后子类调用父类的self.func_hand->self.func。

posted @ 2021-03-06 10:12  快乐码小农  阅读(44)  评论(0编辑  收藏  举报