class File(object):
def __init__(self, filename):
if os.path.isfile(filename):
self.filename = filename
self.file = open(filename, 'rb')
self.__read()
else:
raise Exception('...')
def __read(self):
raise NotImplementedError('Abstract method')
class FileA(File):
def __read(self):
pass
file = FileA('myfile.a')
# NotImplementedError: Abstract method
我的问题:什么是错的?如何将我的代码修复为FileA使用FileA.__read()
来读取文件而不是File.__read()
? :S多态性在Python
预先感谢您。
值得注意的是,通过让基方法引发NotImplementedError破坏多重继承来实现'抽象方法'。使用[abc](http://docs.python.org/library/abc.html)模块是更好的方法。 – aaronasterling 2010-10-19 02:40:15
@Michael Anderson:StackOverflow维护一个完整的更改日志。您不需要发表评论,指出您进行了更改。显而易见,不言而喻。请删除您的评论。 – 2010-10-19 02:42:05