鱼C论坛

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

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

[复制链接]
发表于 2019-11-11 15:32:45 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 zpx1002 于 2019-11-11 16:42 编辑

我先定义了父类 restaurant,
冰淇淋店是其中的子类,
并添加属性flavor,flavor想通过列表列出多个口味,
求助一下各位老师们,我这段代码哪里不对呀。

class Restaurant():
    def __init__(self,restaurant_name,cuisine_type):
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type
    def describe_restaurant(self):
        print(self.restaurant_name)
        print(self.cuisine_type)

class Flavor():
    def __init__(self,ice_flavor):
        self.ice_flavor=ice_flavor
    def describe_flavor(self):
        print(self.ice_flavor)

class Icecream(Restaurant):
    def __init__(self,restaurant_name,cuisine_type):
        super().__init__(restaurant_name,cuisine_type)
        self.flavor=Flavor()

  
restaurant = Icecream('A','B',['milk','apple'])
restaurant.describe_restaurant()
restaurant.flavor.describe_flavor()
                    
谢谢大家!!

我是根据书上的例子写的,还有几个问题想问大家,谢谢耐心解答!
class Car():
----snip----

class Battery():                                   #不是很懂这里为什么要再定义一个类?直接在E_car()下面def 一个函数describe(self)不可以吗???
     def __init__(self, battery_size=70):
     self.battery_size=battery_size
     
     def describe(self):
     print(self.battery_size)

class E_car(Car):
    def __init__(self, make,mode,year):
          super().__init__(make,mode,year)     #为啥这里只输入三个参数呢???
          self.battery=Battery()

mycar=E_car('audi','a6',2010)
mycar.battery.describe()      #这个小写的battery是指我引入的新属性battery吗???
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-11-11 15:33:38 | 显示全部楼层
才刚看到类 这一章。。好多概念还不太懂。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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:08:46 | 显示全部楼层

????不好意思哦,这段代码不是和我写的一模一样吗。。想知道哪里不对呀
小甲鱼最新课程 -> 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:36:50 | 显示全部楼层
本帖最后由 zpx1002 于 2019-11-11 16:42 编辑
danteer 发表于 2019-11-11 16:17
16行和18行
而且我记得13行你也漏了个self的啊?
肯定是有不一样的地方我才发上去的


谢谢!!其实我还是有一点不懂,我是根据书上的例子写的:

class Car():
----snip----

class Battery():
     def __init__(self, battery_size=70):
     self.battery_size=battery_size
     
     def describe(self):
     print(self.battery_size)

class E_car(Car):
    def __init__(self, make,mode,year):
          super().__init__(make,mode,year)
          self.battery=Battery()

mycar=E_car('audi','a6',2010)
mycar.battery.describe()

想问下这里super().__init__(make,mode,year)为什么只输入三个参数,没有battery_size呢?
抱歉问题可能有点愚蠢。。。麻烦您可以教我一下嘛,谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-11 16:37:59 | 显示全部楼层
class Icecream(Restaurant):
    def __init__(self,restaurant_name,cuisine_type):
        super().__init__(restaurant_name,cuisine_type)
        self.flavor=Flavor()  -----> self.flavor=Flavor(ice_flavor)
小甲鱼最新课程 -> 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:49:07 | 显示全部楼层
class Restaurant():
    def __init__(self,restaurant_name,cuisine_type):
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type
    def describe_restaurant(self):
        print(self.restaurant_name)
        print(self.cuisine_type)

class Flavor():
    def __init__(self):
        self.ice_flavor=['milk','apple']
    def describe_flavor(self):
        print(self.ice_flavor)

class Icecream(Restaurant):
    def __init__(self,restaurant_name,cuisine_type):
        super().__init__(restaurant_name,cuisine_type)
        self.flavor=Flavor()

  
restaurant = Icecream('A','B')
restaurant.describe_restaurant()
restaurant.flavor.describe_flavor()


我发现这样好像也可以
小甲鱼最新课程 -> 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-1-20 17:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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