python raise NotImplementedError

NotImplementedError表示未实行的方法

因为注释: Method or function hasn't been implemented yet.

class father():
    def __init__(self):
        self._name = "aaa"

    def getname(self):
        raise NotImplementedError

class son(father):
    def __init__(self):
        father.__init__(self)



if __name__ == "__main__":
    son = son()
    son.getname()

输出:

Traceback (most recent call last):
  File "F:/Pycharm_Projection/Test/test.py", line 26, in <module>
    son.getname()
  File "F:/Pycharm_Projection/Test/test.py", line 11, in getname
    raise NotImplementedError
NotImplementedError

在子类里实现对应方法:

class father():
    def __init__(self):
        self._name = "aaa"

    def getname(self):
        raise NotImplementedError

class son(father):
    def __init__(self):
        father.__init__(self)

    def getname(self):
        print("aaaa")
        return self._name


if __name__ == "__main__":
    son = son()
    son.getname()

输出:

aaaa

猜你喜欢

转载自blog.csdn.net/oMoDao1/article/details/82216702