鱼C论坛

 找回密码
 立即注册
查看: 1685|回复: 0

[技术交流] 《零基础入门学习Python》38 类和对象-继承

[复制链接]
发表于 2017-9-25 00:25:35 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Gabber 于 2017-9-28 18:28 编辑

继承
                        子类                          基类、父类或超类
1. 格式:class DerivedClassName(BaseClassName):
2. 注意:如果子类中定义与父类同名的属性或方法,则会自动覆盖父类对应的属性和方法。
  1. >>> class Parent:
  2.         def hello(self):
  3.                 print("正在调用父类的方法...")               
  4. >>> p = Parent()
  5. >>> p.hello()
  6. 正在调用父类的方法...
  7. >>> class Child(Parent):
  8.         pass
  9. >>> c = Child()
  10. >>> c.hello()
  11. 正在调用父类的方法...
  12. >>> class Child(Parent):
  13.         def hello(self):
  14.                 print("正在调用子类的方法...")
  15. >>> c = Child()
  16. >>> c.hello()
  17. 正在调用子类的方法...
复制代码


3. 调用父类同名的方法:调用未绑定父类的方法、使用super()函数
调用未绑定父类的方法格式,父类名.需要的父类的方法,例如,Parent.__init__(self) 或者 Parent.__init__(子类对象名)
super()函数格式,super().需要的父类的方法, 例如,super().__init__() # 不需要填self

4. 多重继承
格式:class DerivedClassName(Base1,Base2,Base3):

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 15:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表