python并不提供抽象类的写法,但是如果你非要严格实现抽象类,可以使用下面的代码,实际上就是不允许用户直接调用父类的方法,如果用户调用了,则给出错误提示。
class myAbstractClass:
def __init__(self):
if self.__class__ is myAbstractClass:
raise NotImplementedError,"Class %s does not implement __init__(self)" % self.__class__
def method1(self):
raise NotImplementedError,"Class %s does not implement method1(self)" % self.__class__
class myClass(myAbstractClass):
def __init__(self):
pass
m = myClass()
m.method1()
