V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
woshichuanqilz
V2EX  ›  Python

Python 重载问题

  •  
  •   woshichuanqilz · 2022-03-21 21:36:20 +08:00 · 2436 次点击
    这是一个创建于 758 天前的主题,其中的信息可能已经有所发展或是发生改变。

    比如这个代码

    class Bird:
        #鸟有翅膀
        def isWing(self):
            print("鸟有翅膀")
        #鸟会飞
        def fly(self):
            print("鸟会飞")
    class Ostrich(Bird):
        # 重写 Bird 类的 fly()方法
        def fly(self):
            print("鸵鸟不会飞")
    # 创建 Ostrich 对象
    ostrich = Ostrich()
    #调用 Ostrich 类中重写的 fly() 类方法
    ostrich.fly()
    

    父类 bird 中有一个 fly()方法,子类 Ostrich 继承了 bird 类, 子类 Ostrich 中的 fly()方法会覆盖父类 bird 中的 fly()方法,这个如何避免, 我想让子类 Ostrich 中的 fly()方法不覆盖父类 bird 中的 fly()方法,

    我不想修改父类的内容, 因为父类被很多地方调用了

    第 1 条附言  ·  2022-03-22 07:59:51 +08:00
    我只前的代码贴的有点问题忘了加 init 部分:不能表述我的意思, 描述也不太好:

    比如这个代码
    ```
    class Bird:
    def __init__(self) -> None:
    self.fly()

    #鸟有翅膀
    def isWing(self):
    print("鸟有翅膀")
    #鸟会飞
    def fly(self):
    print("鸟会飞")
    class Ostrich(Bird):
    # 重写 Bird 类的 fly()方法
    def fly(self):
    print("鸵鸟不会飞")
    # 创建 Ostrich 对象
    ostrich = Ostrich()

    ```

    创建一个 Ostrich 的时候打印的是"鸵鸟不会飞"(bird 中的 init 打印的), 我想让他打印 bird 自己的 fly , "鸟会飞" 这个怎么操作

    改成 super fly 不行报错:
    `AttributeError: type object 'super' has no attribute 'fly'`
    第 2 条附言  ·  2022-03-22 08:02:56 +08:00
    我的意思主要是如果在 bird 的 init 中有一个 fly , 在创建一个 Ostrich 实例的时候,bird 的 fly 调用的是 Ostrich 的不是 bird 的, 这个如何避免
    9 条回复    2022-03-22 17:54:50 +08:00
    imn1
        1
    imn1  
       2022-03-21 21:48:40 +08:00   ❤️ 1
    类内
    super().fly()

    类外
    Bird().fly()
    JeffGe
        2
    JeffGe  
       2022-03-21 21:54:34 +08:00 via Android
    我不确定我是否明白你的意思,你可能对“覆盖”这个词有误解,子类 Ostrich.fly 方法并不会替换掉 Bird.fly 方法,只是 Ostrich 实例在调用 fly 方法时会优先寻找 Ostrich.fly 方法并调用,从而“覆盖”或者说“屏蔽”了 Bird.fly 方法,其它继承 Bird 类的子类不会受到 Ostrich 方法重载的影响
    ZztGqk
        3
    ZztGqk  
       2022-03-21 22:41:25 +08:00
    话说这不是叫重写( Override )么。还有应该需要先理解一下多态。
    omtow
        4
    omtow  
       2022-03-21 22:50:20 +08:00 via iPhone
    你可以了解下 Python 的 MRO (Method Resolution Order),可能会对你有帮助
    BeautifulSoap
        5
    BeautifulSoap  
       2022-03-21 23:03:22 +08:00   ❤️ 1
    1. LZ 你可能误解了“重载”这个词的意思,建议学习下(不如说 python 就没有重载这个概念)

    2. lz 问题的答案: :super(Bird, ostrich).fly()
    ixuuux
        6
    ixuuux  
       2022-03-21 23:03:38 +08:00 via iPhone
    在 print(“鸵鸟不会飞”)的上面,加上一句 super().fly()。会完整执行父类的 fly()
    Leviathann
        7
    Leviathann  
       2022-03-21 23:12:28 +08:00
    不会修改被继承的类的。。
    ospider
        8
    ospider  
       2022-03-22 08:31:55 +08:00
    现在已经不流行 inheritance 了,更倾向于 composition 。
    lizytalk
        9
    lizytalk  
       2022-03-22 17:54:50 +08:00
    用 super 是正解,只是你写的不对而已,楼上的几位老哥说的很对。
    这个不叫重载,重载是 overload ,这个是 override
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5278 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 07:43 · PVG 15:43 · LAX 00:43 · JFK 03:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.