In [1]:
class ClassDemo:
def test_demo(self):
print('如果要求其子类一定要实现,不实现的时候会导致问题,那么采用raise的方式就很好。')
raise NotImplementedError("my test: not implemented!此处应当使用NotImplementedError")
pass
class ChildClass1(ClassDemo):
def test_demo(self):
print('子类实现,就不会报错。')
pass
class ChildClass2(ClassDemo):
pass
In [2]:
# 子类实现,就不会报错。
inst =ChildClass1()
inst.test_demo()
In [3]:
# 不实现的时候会导致问题
inst =ChildClass2()
inst.test_demo()