鱼C论坛

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

[学习笔记] 038类和对象:继承

[复制链接]
发表于 2017-7-14 21:39:19 | 显示全部楼层 |阅读模式

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

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

x
1.继承:一个类使用前面一个类所有的方法和属性
class DerivedClassName(BaseClassName):
                ......
继承者:子类;被继承者:基类、父类或超类
  1. >>> class Parent:
  2.         def hello(self):
  3.                 print("正在调用父类的方法...")

  4.                
  5. >>> class Child(Parent):
  6.         pass

  7. >>> p = Parent()
  8. >>> p.hello()
  9. 正在调用父类的方法...
  10. >>> c = Child()
  11. >>> c.hello()
  12. 正在调用父类的方法...
  13. >>> class Child(Parent):
  14.         def hello(self):
  15.                 print("正在调用子类的方法...")

  16.                
  17. >>> c = Child()
  18. >>> c.hello()
  19. 正在调用子类的方法...
  20. >>> p.hello()
  21. 正在调用父类的方法...
复制代码

注意:如果子类中定义与父类同名的方法或属性,则会自动覆盖父类对应的方法或属性。
  1. import random as r
  2. class Fish:
  3.     def __init__(self):
  4.         self.x = r.randint(0, 10)
  5.         self.y = r.randint(0, 10)

  6.     def move(self):
  7.         self.x -= 1
  8.         print("我的位置是:", self.x, self.y)

  9. class Goldfish(Fish):
  10.     pass

  11. class Carp(Fish):
  12.     pass

  13. class Salmon(Fish):
  14.     pass

  15. class Shark(Fish):
  16.     def __init__(self):
  17.         self.hungry = True

  18.     def eat(self):
  19.         if self.hungry:
  20.             print("吃货的梦想就是天天有的吃^_^")
  21.             self.hungry = False
  22.         else:
  23.             print("太撑了,吃不下了!")

  24. >>> fish = Fish()
  25. >>> fish.move()
  26. 我的位置是: 7 6
  27. >>> fish.move()
  28. 我的位置是: 6 6
  29. >>> goldfish = Goldfish()
  30. >>> goldfish.move()
  31. 我的位置是: 9 7
  32. >>> goldfish.move()
  33. 我的位置是: 8 7
  34. >>> goldfish.move()
  35. 我的位置是: 7 7
  36. >>> shark = Shark()
  37. >>> shark.eat()
  38. 吃货的梦想就是天天有的吃^_^
  39. >>> shark.eat()
  40. 太撑了,吃不下了!
  41. >>> shark.move()
  42. Traceback (most recent call last):
  43.   File "<pyshell#11>", line 1, in <module>
  44.     shark.move()
  45.   File "F:\BaiduYunDownload\《零基础入门学习Python》\py\038-1.py", line 8, in move
  46.     self.x -= 1
  47. AttributeError: 'Shark' object has no attribute 'x'
复制代码


1)调用未绑定的父类方法
  1. import random as r
  2. class Fish:
  3.     def __init__(self):
  4.         self.x = r.randint(0, 10)
  5.         self.y = r.randint(0, 10)

  6.     def move(self):
  7.         self.x -= 1
  8.         print("我的位置是:", self.x, self.y)

  9. class Goldfish(Fish):
  10.     pass

  11. class Carp(Fish):
  12.     pass

  13. class Salmon(Fish):
  14.     pass

  15. class Shark(Fish):
  16.     def __init__(self):
  17.         Fish.__init__(self)
  18.         self.hungry = True

  19.     def eat(self):
  20.         if self.hungry:
  21.             print("吃货的梦想就是天天有的吃^_^")
  22.             self.hungry = False
  23.         else:
  24.             print("太撑了,吃不下了!")

  25. >>> shark = Shark()
  26. >>> shark.move()
  27. 我的位置是: 3 1
  28. >>> shark.move()
  29. 我的位置是: 2 1
  30. >>> Fish.__init__()
  31. Traceback (most recent call last):
  32.   File "<pyshell#15>", line 1, in <module>
  33.     Fish.__init__()
  34. TypeError: __init__() missing 1 required positional argument: 'self'
  35. >>> Fish.__init__(shark)
  36. >>> shark.move()
  37. 我的位置是: 6 4
复制代码

2)使用super函数
  1. import random as r
  2. class Fish:
  3.     def __init__(self):
  4.         self.x = r.randint(0, 10)
  5.         self.y = r.randint(0, 10)

  6.     def move(self):
  7.         self.x -= 1
  8.         print("我的位置是:", self.x, self.y)

  9. class Goldfish(Fish):
  10.     pass

  11. class Carp(Fish):
  12.     pass

  13. class Salmon(Fish):
  14.     pass

  15. class Shark(Fish):
  16.     def __init__(self):
  17.         super().__init__()
  18.         self.hungry = True

  19.     def eat(self):
  20.         if self.hungry:
  21.             print("吃货的梦想就是天天有的吃^_^")
  22.             self.hungry = False
  23.         else:
  24.             print("太撑了,吃不下了!")
  25. >>> shark = Shark()
  26. >>> shark.move()
  27. 我的位置是: 3 3
复制代码

2.多重继承:可以同时继承多个父类的属性和方法
  1. class DerivedClassName(Base1, Base2, Base3):
  2.                   ......
  3. >>> class Base1:
  4.         def foo1(self):
  5.                 print("我是foo1,我为Base1代言...")

  6.                
  7. >>> class Base2:
  8.         def foo2(self):
  9.                 print("我是foo2,我为Base2代言...")

  10.                
  11. >>> class C(Base1, Base2):
  12.         pass

  13. >>> c = C()
  14. >>> c.foo1()
  15. 我是foo1,我为Base1代言...
  16. >>> c.foo2()
  17. 我是foo2,我为Base2代言...
复制代码

评分

参与人数 1荣誉 +5 鱼币 +5 收起 理由
小甲鱼 + 5 + 5 支持楼主!

查看全部评分

本帖被以下淘专辑推荐:

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-28 03:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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