1.用在抽象方法中
注意,python的抽象方法的实现需要借助装饰器来实现。在抽象方法中加上NotImplementedError("XXXX"),如果子类没有实现父类的抽象方法,就会报错NotImplementedError:XXXX
import abc
class father(object):
@abc.abstractmethod# 装饰器,说明该方法是抽象方法
def a(self):
raise NotImplementedError("method is not implemented")# 自己的方法还没有实现则报错
class sonfirst(father):
def a(self):
print("son类实现a方法")
class sonsecond(father):
pass
if __name__ == '__main__':
test = sonfirst()
test.a()# 输出:son类实现a方法
test = sonsecond()
test.a()# 报错:NotImplementedError: method is not implemente