<tfoot id='iUlj0'></tfoot>
    <i id='iUlj0'><tr id='iUlj0'><dt id='iUlj0'><q id='iUlj0'><span id='iUlj0'><b id='iUlj0'><form id='iUlj0'><ins id='iUlj0'></ins><ul id='iUlj0'></ul><sub id='iUlj0'></sub></form><legend id='iUlj0'></legend><bdo id='iUlj0'><pre id='iUlj0'><center id='iUlj0'></center></pre></bdo></b><th id='iUlj0'></th></span></q></dt></tr></i><div id='iUlj0'><tfoot id='iUlj0'></tfoot><dl id='iUlj0'><fieldset id='iUlj0'></fieldset></dl></div>
  1. <legend id='iUlj0'><style id='iUlj0'><dir id='iUlj0'><q id='iUlj0'></q></dir></style></legend>

      <small id='iUlj0'></small><noframes id='iUlj0'>

      • <bdo id='iUlj0'></bdo><ul id='iUlj0'></ul>

      Python中的多态性

      Polymorphism in Python(Python中的多态性)
      <i id='96TeF'><tr id='96TeF'><dt id='96TeF'><q id='96TeF'><span id='96TeF'><b id='96TeF'><form id='96TeF'><ins id='96TeF'></ins><ul id='96TeF'></ul><sub id='96TeF'></sub></form><legend id='96TeF'></legend><bdo id='96TeF'><pre id='96TeF'><center id='96TeF'></center></pre></bdo></b><th id='96TeF'></th></span></q></dt></tr></i><div id='96TeF'><tfoot id='96TeF'></tfoot><dl id='96TeF'><fieldset id='96TeF'></fieldset></dl></div>

        <tfoot id='96TeF'></tfoot>

        <small id='96TeF'></small><noframes id='96TeF'>

          <tbody id='96TeF'></tbody>

          <legend id='96TeF'><style id='96TeF'><dir id='96TeF'><q id='96TeF'></q></dir></style></legend>
              <bdo id='96TeF'></bdo><ul id='96TeF'></ul>
              • 本文介绍了Python中的多态性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                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

                My question: what's wrong? How I can fix my code to FileA use FileA.__read() to read the file instead of File.__read()? :S

                提前谢谢你.

                推荐答案

                用双下划线前缀属性不会使属性私有,它只会使多态性变得不可能,因为属性名称与 当前 类名.改为将其更改为单个下划线前缀.

                Prefixing an attribute with double underscores doesn't make the attribute private, it simply makes polymorphism impossible because the attribute name gets mangled with the current class name. Change it to a single underscore prefix instead.

                这篇关于Python中的多态性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)

              • <i id='oSuFy'><tr id='oSuFy'><dt id='oSuFy'><q id='oSuFy'><span id='oSuFy'><b id='oSuFy'><form id='oSuFy'><ins id='oSuFy'></ins><ul id='oSuFy'></ul><sub id='oSuFy'></sub></form><legend id='oSuFy'></legend><bdo id='oSuFy'><pre id='oSuFy'><center id='oSuFy'></center></pre></bdo></b><th id='oSuFy'></th></span></q></dt></tr></i><div id='oSuFy'><tfoot id='oSuFy'></tfoot><dl id='oSuFy'><fieldset id='oSuFy'></fieldset></dl></div>
                  <bdo id='oSuFy'></bdo><ul id='oSuFy'></ul>
                  <tfoot id='oSuFy'></tfoot>
                • <legend id='oSuFy'><style id='oSuFy'><dir id='oSuFy'><q id='oSuFy'></q></dir></style></legend>

                      <tbody id='oSuFy'></tbody>

                        <small id='oSuFy'></small><noframes id='oSuFy'>