鱼C论坛

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

[学习笔记] 039类和对象:拾遗

[复制链接]
发表于 2017-7-15 22:52:32 | 显示全部楼层 |阅读模式

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

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

x
1.组合:把类的实例化放到新类里面,那么就能把新类组合进去了
  1. class Turtle:
  2.     def __init__(self, x):
  3.         self.num = x

  4. class Fish:
  5.     def __init__(self, x):
  6.         self.num = x

  7. class Pool:
  8.     def __init__(self, x, y):
  9.         self.turtle = Turtle(x)
  10.         self.fish = Fish(y)

  11.     def print_num(self):
  12.         print("水池里总共有乌龟 %d 只,小鱼 %d 条!" % (self.turtle.num, self.fish.num))

  13. >>> pool = Pool(1, 10)
  14. >>> pool.print_num()
  15. 水池里总共有乌龟 1 只,小鱼 10 条!
复制代码

2.类、类对象和实例对象
类定义                 C
类对象                 C
实例对象     a         b         c
  1. >>> class C:
  2.         count = 0

  3.        
  4. >>> a = C()
  5. >>> b = C()
  6. >>> c = C()
  7. >>> a.count
  8. 0
  9. >>> b.count
  10. 0
  11. >>> c.count
  12. 0
  13. >>> c.count += 10
  14. >>> c.count
  15. 10
  16. >>> a.count
  17. 0
  18. >>> b.count
  19. 0
  20. >>> C.count
  21. 0
  22. >>> C.count += 100
  23. >>> a.count
  24. 100
  25. >>> b.count
  26. 100
  27. >>> c.count
  28. 10
复制代码

  1. >>> class C:
  2.         def x(self):
  3.                 print("X-man!")

  4.                
  5. >>> c = C()
  6. >>> c.x()
  7. X-man!
  8. >>> c.x=1
  9. >>> c.x
  10. 1
  11. >>> c.x()
  12. Traceback (most recent call last):
  13.   File "<pyshell#8>", line 1, in <module>
  14.     c.x()
  15. TypeError: 'int' object is not callable
复制代码

注意:
1)不要试图在一个类里边定义出所有能想到的特性和方法,应该利用继承和组合机制来进行扩展。
2)用不同的词性命名,如属性名用名词,方法名用动词。

3.绑定:Python严格要求方法需要有实例才能被调用,这种限制其实就是Python所谓的绑定概念。
  1. >>> class BB:
  2.         def printBB():
  3.                 print("no zuo no die")

  4.                
  5. >>> BB.printBB()
  6. no zuo no die
  7. >>> bb = BB()
  8. >>> bb.printBB()
  9. Traceback (most recent call last):
  10.   File "<pyshell#15>", line 1, in <module>
  11.     bb.printBB()
  12. TypeError: printBB() takes 0 positional arguments but 1 was given
  13. >>> class CC:
  14.         def setXY(self, x, y):
  15.                 self.x = x
  16.                 self.y = y
  17.         def printXY(self):
  18.                 print(self.x, self.y)

  19.                
  20. >>> dd = CC()
  21. >>> dd.__dict__
  22. {}
  23. >>> CC.__dict__
  24. mappingproxy({'__dict__': <attribute '__dict__' of 'CC' objects>, '__weakref__': <attribute '__weakref__' of 'CC' objects>, 'printXY': <function CC.printXY at 0x024D8270>, '__module__': '__main__', 'setXY': <function CC.setXY at 0x024D8228>, '__doc__': None})
  25. >>> dd.setXY(4, 5)
  26. >>> dd.__dict__
  27. {'x': 4, 'y': 5}
  28. >>> CC.__dict__
  29. mappingproxy({'__dict__': <attribute '__dict__' of 'CC' objects>, '__weakref__': <attribute '__weakref__' of 'CC' objects>, 'printXY': <function CC.printXY at 0x024D8270>, '__module__': '__main__', 'setXY': <function CC.setXY at 0x024D8228>, '__doc__': None})
  30. >>> del CC
  31. >>> ee = CC()
  32. Traceback (most recent call last):
  33.   File "<pyshell#31>", line 1, in <module>
  34.     ee = CC()
  35. NameError: name 'CC' is not defined
  36. >>> dd.printXY()
  37. 4 5
复制代码

评分

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

查看全部评分

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-4 07:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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