鱼C论坛

 找回密码
 立即注册
查看: 2102|回复: 9

关于 类与对象 这一章的问题,求助!

[复制链接]
发表于 2019-11-11 15:50:04 | 显示全部楼层
  1. class Restaurant():
  2.     def __init__(self,restaurant_name,cuisine_type):
  3.         self.restaurant_name = restaurant_name
  4.         self.cuisine_type = cuisine_type
  5.     def describe_restaurant(self):
  6.         print(self.restaurant_name)
  7.         print(self.cuisine_type)

  8. class Flavor():
  9.     def __init__(self,ice_flavor):
  10.         self.ice_flavor=ice_flavor
  11.     def describe_flavor(self):
  12.         print(self.ice_flavor)

  13. class Icecream(Restaurant):
  14.     def __init__(self,restaurant_name,cuisine_type,ice_flavor):
  15.         super().__init__(restaurant_name,cuisine_type)
  16.         self.flavor=Flavor(ice_flavor)

  17.   
  18. restaurant = Icecream('A','B',['milk','apple'])
  19. restaurant.describe_restaurant()
  20. restaurant.flavor.describe_flavor()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-11 16:17:53 | 显示全部楼层
本帖最后由 danteer 于 2019-11-11 16:20 编辑
zpx1002 发表于 2019-11-11 16:08
????不好意思哦,这段代码不是和我写的一模一样吗。。想知道哪里不对呀


16行和18行
而且我记得13行你也漏了个self的啊?
肯定是有不一样的地方我才发上去的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-11 16:46:41 | 显示全部楼层
zpx1002 发表于 2019-11-11 16:36
谢谢!!其实我还是有一点不懂,我是根据书上的例子写的:

class Car():

battery_size有默认值,不输入的话就等于默认值
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-11 16:52:42 | 显示全部楼层
zpx1002 发表于 2019-11-11 16:49
class Restaurant():
    def __init__(self,restaurant_name,cuisine_type):
        self.restaurant_n ...

但是你的flavor就是固定的了,不能在定义的时候通过不同的输入来更改
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-1 11:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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