鱼C论坛

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

[学习笔记] 2022-10-08 学习Python-Day16

[复制链接]
发表于 2022-10-8 16:44:46 | 显示全部楼层 |阅读模式

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

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

x
国庆归来,学习类与对象开始P59-P61
  1. ###########################################P59 对象与类
  2. '''
  3. 20221008
  4. '''
  5. class Turtle:
  6.     head=1
  7.     eyes=2
  8.     legs=4
  9.     shell=True
  10.     def crawl(self):
  11.         print('11')
  12.     def run(self):
  13.         print('22')
  14.     def bite(self):
  15.         print('33')
  16.     def eat(self):
  17.         print('44')
  18.     def sleep(self):
  19.         print('55')
  20. t1 =Turtle()
  21. print(t1.head)
  22. t1.crawl()
  23. t2=Turtle()
  24. t2.legs=321
  25. print(t2.legs)
  26. print(t1.legs)
  27. t1.mouth=5
  28. print(t1.mouth)
  29. print(dir(t1))
  30. print(dir(t2))
  31. x=520
  32. print(type(x))
  33. y='Fishc'
  34. print(type(y))
  35. class C:
  36.     def hello(self):
  37.         print(self)
  38. c=C()
  39. print(c)
  40. c.hello()
  41. ###########################################P60 对象与类
  42. class A:
  43.     x=520
  44.     def hello(self):
  45.         print('你好,我是A')
  46. class B(A):
  47.     pass
  48. b=B()
  49. print(b.x)
  50. b.hello()
  51. class B(A):
  52.     x=880
  53.     def hello(self):
  54.         print('你不好,我是B')
  55. b=B()
  56. b.hello()
  57. print(isinstance(b,B))
  58. print(isinstance(b,A))
  59. print(issubclass(A,B))
  60. print(issubclass(B,A))
  61. class B:
  62.     x=520
  63.     y=250
  64.     def hello(self):
  65.         print('你好,我是B')
  66. class C(A,B):
  67.     pass
  68. c=C()
  69. print(c.x)
  70. print(c.y)
  71. c.hello()
  72. class Turtle:
  73.     def say(self):
  74.         print('不不不')

  75. class Cat:
  76.     def say(self):
  77.         print('喵喵喵')

  78. class Dog:
  79.     def say(self):
  80.         print('汪汪汪')

  81. class Garden:
  82.     t=Turtle()
  83.     c=Cat()
  84.     d=Dog()
  85.     def say(self):
  86.         self.t.say()
  87.         self.c.say()
  88.         self.d.say()
  89. g=Garden()
  90. print(g)
  91. g1=Garden
  92. print(g1)
  93. g.say()
  94. ###########################################P60 对象与类
  95. d =C()
  96. d.x=2500
  97. c.x=250
  98. print(d.__dict__)
  99. print(c.__dict__)
  100. d.y=660
  101. print(d.__dict__)
  102. class C:
  103.     def set_x(self,v):
  104.         self.x=v
  105. c=C()
  106. print(c.__dict__)
  107. c.set_x(5200)
  108. print(c.__dict__)
  109. class C:
  110.     x=100
  111.     def set_x(self,v):
  112.         x=v
  113. c=C()
  114. c.set_x(250)
  115. print(c.x)
  116. C.x=2500
  117. print(c.x)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-23 07:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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