如何将类继承扩展到其他功能

时间:2019-02-08 05:41:04

标签: python-3.x inheritance

我有以下课程:

class c0(object):
    def post_pred(self, ins):
        print("this is post_pred in c0")

class c1(c0):

    def __init__(self):
        print("this is class c1")

    def predict(self, ins):
        print("this is predict in c1")
        selfz.post_pred(0)

    def post_pred(self,ins):
        print("this is post_pred in c1")



class c2(c1):

    def __init__(self):
        super().__init__()
        print("this is class c2")

    def predict(self, ins):
        super().predict(0)
        print("this is predict in c2")
        self.post_pred(0)

    def post_pred(self, ins):
        print("this is post_pred in c2")


if __name__ == '__main__':
    sss = c2()

    sss.predict(2)

我想看到的是运行此脚本后,看到以下输出:

this is class c1
this is class c2
this is predict in c1
this is post_pred in c1
this is predict in c2
this is post_pred in c2

但是我看到了:

this is class c1
this is class c2
this is predict in c1
this is post_pred in c2
this is predict in c2
this is post_pred in c2

似乎继承没有扩展到post_pred函数。我只是想知道实现我的目标的正确方法。

0 个答案:

没有答案