鱼C论坛

 找回密码
 立即注册
查看: 1512|回复: 6

[已解决]类的属性可以存放列表吗?如果可以,如何在方法中调用呢?

[复制链接]
发表于 2021-8-7 14:35:45 | 显示全部楼层 |阅读模式

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

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

x
类的属性可以存放列表吗?如果可以,如何在方法中调用呢?
最佳答案
2021-8-7 15:14:20
你这属性和方法都叫flavors,程序也很迷惑
  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(f' the restaurant name is {self.restaurant_name}')
  7.         print(f'this restaurant sales {self.cuisine_type} food')
  8.     def open_restaurant(self):
  9.         print('the restaurant is opening')


  10. class Icecreamstand(Restaurant):
  11.     def __init__(self,restaurant_name,cuisine_type):
  12.         super().__init__(restaurant_name,cuisine_type)
  13.         self.flavors1=['hu','df''jk']
  14.         
  15.     def flavors(self):
  16.         print(f'there are flavors as bolew:{self.flavors1}')
  17.         
  18. eg=Icecreamstand('holiwood','icecream')
  19. eg.describe_restaurant()
  20. eg.open_restaurant()
  21. eg.flavors()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-7 14:56:29 | 显示全部楼层
  1. class Test():
  2.     lst = [0,1,2,3,4]
  3.     def test():
  4.         for i in Test.lst:
  5.             print(i)
  6.             
  7. Test.test()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-8-7 15:10:15 | 显示全部楼层

class Restaurant:
    def __init__(self,restaurant_name,cuisine_type):
        self.restaurant_name=restaurant_name
        self.cuisine_type=cuisine_type
    def describe_restaurant(self):
        print(f' the restaurant name is {self.restaurant_name}')
        print(f'this restaurant sales {self.cuisine_type} food')
    def open_restaurant(self):
        print('the restaurant is opening')


class Icecreamstand(Restaurant):
    def __init__(self,restaurant_name,cuisine_type):
        super().__init__(restaurant_name,cuisine_type)
        self.flavors=['hu','df''jk']
        
    def flavors(self):
        print(f'there are flavors as bolew:{self.flavors}')
        
eg=Icecreamstand('holiwood','icecream')
eg.describe_restaurant()
eg.open_restaurant()
eg.flavors()////////  TypeError: 'list' object is not callable这个为什么不对呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-7 15:10:47 | 显示全部楼层
你的意思是在类里放个列表存放属性,还是类外用个列表放属性? 这两个都可以实现
你是说用类的方法调用方法还是属性,这个好像也行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-7 15:14:20 | 显示全部楼层    本楼为最佳答案   
你这属性和方法都叫flavors,程序也很迷惑
  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(f' the restaurant name is {self.restaurant_name}')
  7.         print(f'this restaurant sales {self.cuisine_type} food')
  8.     def open_restaurant(self):
  9.         print('the restaurant is opening')


  10. class Icecreamstand(Restaurant):
  11.     def __init__(self,restaurant_name,cuisine_type):
  12.         super().__init__(restaurant_name,cuisine_type)
  13.         self.flavors1=['hu','df''jk']
  14.         
  15.     def flavors(self):
  16.         print(f'there are flavors as bolew:{self.flavors1}')
  17.         
  18. eg=Icecreamstand('holiwood','icecream')
  19. eg.describe_restaurant()
  20. eg.open_restaurant()
  21. eg.flavors()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-8-7 15:15:31 | 显示全部楼层
大马强 发表于 2021-8-7 15:10
你的意思是在类里放个列表存放属性,还是类外用个列表放属性? 这两个都可以实现
你是说用类的方法调用方法 ...

在类里面用列表存放属性然后再用方法调用,就看上面这个例子哪里有问题呢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-8-7 15:18:08 | 显示全部楼层
逃兵 发表于 2021-8-7 15:14
你这属性和方法都叫flavors,程序也很迷惑

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-25 09:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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