鱼C论坛

 找回密码
 立即注册
楼主: 小甲鱼

[课后作业] 第063讲:类和对象(VI)| 课后测试题及答案

  [复制链接]
发表于 2023-2-1 15:48:02 | 显示全部楼层
零基础入门学习Python封面
《零基础入门学习Python》
(最新版)
小甲鱼 著
立即购买
半年咯
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-1-31 17:03:06 | 显示全部楼层
半年了,还没更
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-1-28 20:56:48 | 显示全部楼层
催更+1
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-1-12 19:51:10 | 显示全部楼层
催更
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-10-14 14:42:14 | 显示全部楼层
求更求更求更!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-8-29 11:52:31 | 显示全部楼层
多态
  1. class Dog:
  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("呦吼~")

  9. class Cat:
  10.     def __init__(self,name,age):
  11.         self.name = name
  12.         self.age = age

  13.     def intro(self):
  14.         print(f"我是一只小猫,我叫{self.name},今年{self.age}岁")

  15.     def say(self):
  16.         print("喵喵~")

  17. class Bird:
  18.     def __init__(self,name,age):
  19.         self.name = name
  20.         self.age = age

  21.     def intro(self):
  22.         print(f"我是一只小鸟,我叫{self.name},今年{self.age}岁")

  23.     def say(self):
  24.         print("唧唧~")

  25. c = Cat("布偶",2)
  26. d = Dog("狗狗",2)
  27. b = Bird("鸟鸟",5)

  28. def animal(c):
  29.     c.intro()
  30.     c.say()

  31. animal(c)
  32. animal(d)
  33. animal(b)
复制代码

  1. =============== RESTART: D:/Python.test/test222.py ===============
  2. 我是一只小猫,我叫布偶,今年2岁
  3. 喵喵~
  4. 我是一只小狗,我叫狗狗,今年2岁
  5. 呦吼~
  6. 我是一只小鸟,我叫鸟鸟,今年5岁
  7. 唧唧~

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

使用道具 举报

发表于 2022-8-29 11:23:32 | 显示全部楼层
本帖最后由 山小 于 2022-8-29 11:25 编辑

看看笔记解闷儿吧
  1. class Shape:
  2.     def __init__(self,name):
  3.         self.name = name

  4.     def area(self):
  5.         pass

  6. class Square(Shape):
  7.     def __init__(self,length):
  8.         super().__init__("正方形")
  9.         self.length = length

  10.     def area(self):
  11.         return self.length * self.length

  12. class Circle(Shape):
  13.     def __init__(self,radius):
  14.         super().__init__("圆形")
  15.         self.radius = radius

  16.     def area(self):
  17.         return 3.14 * self.radius * self.radius

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

  25. s = Square(5)
  26. c = Circle(6)
  27. t = Triangle(3,4)

  28. print(f"s = {s.__dict__}")
  29. print(f"s.area = {s.area()}")
  30. print(f"c = {c.__dict__}")
  31. print(f"c.area = {c.area()}")
  32. print(f"t = {t.__dict__}")
  33. print(f"t.area = {t.area()}")
复制代码

  1. =============== RESTART: D:/Python.test/test221.py ===============
  2. s = {'name': '正方形', 'length': 5}
  3. s.area = 25
  4. c = {'name': '圆形', 'radius': 6}
  5. c.area = 113.03999999999999
  6. t = {'name': '三角形', 'base': 3, 'height': 4}
  7. t.area = 6.0
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-8-21 11:30:02 | 显示全部楼层
老大求更新作业呀!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-8-21 12:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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