鱼C论坛

 找回密码
 立即注册
查看: 3359|回复: 7

[知识点备忘] 第063讲:类和对象(VI)

[复制链接]
发表于 2022-6-6 01:09:53 | 显示全部楼层 |阅读模式
购买主题 已有 25 人购买  本主题需向作者支付 5 鱼币 才能浏览
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-9-4 15:36:16 | 显示全部楼层
  1. class Shape:
  2.     def __init__(self,name):
  3.         self.name=name
  4.     def area(self):
  5.         pass

  6.    
  7. class Square(Shape):
  8.     def __init__(self,length):
  9.         super().__init__("正方形")
  10.         self.length=length
  11.     def area(self):
  12.         return self.length*self.length

  13.    
  14. class Circle(Shape):
  15.     def __init__(self,radius):
  16.         super().__init__("圆形")
  17.         self.radius=radius
  18.     def area(self):
  19.         return 3.14*self.radius*self.radius

  20.    
  21. class Triangle(Shape):
  22.     def __init__(self,base,height):
  23.         super().__init__("三角形")
  24.         self.base=base
  25.         self.height=height
  26.     def area(self):
  27.         return self.base*self.height/2

  28.    
  29. s=Square(5)
  30. c=Circle(6)
  31. t=Triangle(3,4)
  32. s.name
  33. '正方形'
  34. c.name
  35. '圆形'
  36. t.name
  37. '三角形'
  38. s.area()
  39. 25
  40. c.area()
  41. 113.03999999999999
  42. t.area()
  43. 6.0
复制代码

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

使用道具 举报

发表于 2022-9-4 15:37:16 | 显示全部楼层
  1. class Cat:
  2.     def __init__(self,name,age):
  3.         self.name=name
  4.         self.age=age
  5.     def intro(self):
  6.         print(f"我是一只沙雕猫咪,我叫{self.name},今年{self.age}岁。")
  7.     def say(self):
  8.         print("mua")

  9.         
  10. class Dog:
  11.     def __init__(self,name,age):
  12.         self.name=name
  13.         self.age=age
  14.     def intro(self):
  15.         print(f"我是一只小狗,我叫{self.name},今年{self.age}岁。")
  16.     def say(self):
  17.         print("呦吼")

  18.         
  19. class Pig:
  20.     def __init__(self,name,age):
  21.         self.name=name
  22.         self.age=age
  23.     def intro(self):
  24.         print(f"我是一只小猪,我叫{self.name},今年{self.age}岁。")
  25.     def say(self):
  26.         print("oink")

  27.         
  28. c=Cat("web",4)
  29. d=Dog("布布",7)
  30. p=Pig("大肠",5)
  31. def animal(x):
  32.     x.intro()
  33.     x.say()

  34.    
  35. animal(c)
  36. 我是一只沙雕猫咪,我叫web,今年4岁。
  37. mua
  38. animal(d)
  39. 我是一只小狗,我叫布布,今年7岁。
  40. 呦吼
  41. animal(p)
  42. 我是一只小猪,我叫大肠,今年5岁。
  43. oink


  44. class Bicycle:
  45.     def intro(self):
  46.         print("我曾经跨过山和大海,也穿过人山人海")
  47.     def say(self):
  48.         print("都有自行车了,要什么兰博基尼?")

  49.         
  50. b=Bicycle()
  51. animal(b)
  52. 我曾经跨过山和大海,也穿过人山人海
  53. 都有自行车了,要什么兰博基尼?
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-9-28 09:42:47 | 显示全部楼层
本节讲到了OOP的第三个基本特征——多态。所谓多态,就是指同一个运算符、函数或对象在不同场景下具有不同的作用效果的特点。换言之,多态为不同数据类型的实体提供了统一的接口,或使用单一的符号来表示多个不同的类型,见机行事,一目了然。重写其实就是实现类继承的多态,还可以自定义函数来实现多态接口,接受不同对象作为参数,并且在不检查参数类型的情况下执行相应的方法,使用起来方便灵活,顺理成章,理所当然!有趣的“鸭子类型”正是如此,在编程中我们往往并不关心对象是什么类型,关心的是它的行为是否符合要求;只要一只鸟的行为像鸭子,我们就可以称之为“鸭子”!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2022-11-15 15:18:38 | 显示全部楼层
Learning...
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-1-2 21:08:01 | 显示全部楼层
本帖最后由 Ensoleile 于 2023-1-10 00:37 编辑

多态和鸭子类型
  1. #多态 -> 见机行事的行为
  2. print(3 + 5)
  3. print('learning' + 'python')
  4. #同样是‘+’,效果不同
  5. #有关类继承的方法,其实重写就是实现类继承的多态
  6. class Shape:
  7.     def __init__(self, name):
  8.         self.name = name
  9.     def area(self):
  10.         pass

  11. class Square(Shape):
  12.     def __init__(self, length):
  13.         super().__init__('正方形')
  14.         self.length = length
  15.     def area(self):
  16.         return self.length ** 2

  17. class Circle(Shape):
  18.     def __init__(self, radius):
  19.         super().__init__('圆形')
  20.         self.radius = radius
  21.     def area(self):
  22.         return 3.14 * self.radius * self.radius

  23. class Triangle(Shape):
  24.     def __init__(self, base, height):
  25.         super().__init__('三角形')
  26.         self.base = base
  27.         self.height = height
  28.     def area(self):
  29.         return self.base * self.height / 2

  30. s = Square(5)
  31. c = Circle(5)
  32. t = Triangle(3, 4)
  33. print(s.name, c.name, t.name, sep='\n')
  34. print(s.area(), c.area(), t.area(), sep='\n')

  35. class Cat:
  36.     def __init__(self, name, age):
  37.         self.name = name
  38.         self.age = age
  39.     def intro(self):
  40.         print(f'我是一只猫咪,我叫{self.name},今年{self.age}岁。')
  41.     def say(self):
  42.         print('mau~')

  43. class Dog:
  44.     def __init__(self, name, age):
  45.         self.name = name
  46.         self.age = age
  47.     def intro(self):
  48.         print(f'我是一只小狗,我叫{self.name},今年{self.age}岁。')
  49.     def say(self):
  50.         print('wang~')

  51. class Pig:
  52.     def __init__(self, name, age):
  53.         self.name = name
  54.         self.age = age
  55.     def intro(self):
  56.         print(f'我是一只小猪,我叫{self.name},今年{self.age}岁。')
  57.     def say(self):
  58.         print('oink~')

  59. c = Cat('web', 4)
  60. d = Dog('布布', 7)
  61. p = Pig('大肠', 5)

  62. def animal(x):
  63.     x.intro()
  64.     x.say()

  65. animal(c)
  66. animal(d)
  67. animal(p)

  68. #animal函数具有多态性,该函数接收不同的对象作为参数,并且不检查类型的情况下执行它的方法
  69. #鸭子类型:在编程中,我们并不会关心对象是什么类型,我们关心的是它的行为是否符合要求  
  70. class Bicycle():
  71.     def intro(self):
  72.         print('我曾经跨过山河大海,也穿过人山人海~')
  73.     def say(self):
  74.         print('都有自行车了,要什么玛莎拉蒂?')

  75. b = Bicycle()
  76. animal(b)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-1-12 21:42:57 | 显示全部楼层
打卡
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-3-30 15:56:20 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-24 17:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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