llllllxxx 2019-10-09 23:06 采纳率: 0%
浏览 444

Python将实例用作属性 里面的方法修改不了属性

class Restaurant():
"""餐馆"""

def __init__(self,restaurant_name,*cuisine_type):
    """初始化餐换"""

    self.restaurant_name=restaurant_name
    self.cuisine_type=cuisine_type

def describe_restaurant(self):
    """打印餐馆信息"""
    print("The restaurant's name is "+ self.restaurant_name)
    print("The restaurant have the following cuisine_type "+ str(self.cuisine_type))

def open_restaurant(self):
    """营业"""
    print("The "+self.restaurant_name+" is Open\n")

restaurant=Restaurant('餐馆','食物')
restaurant.describe_restaurant()
restaurant.open_restaurant()

class Show():
"""列出列表"""

def __init__(self,flavors):
    self.flavors=flavors

def show_flavors(self):
    """列出口味"""
    print("There are the following flavors:")
    for flavor in self.flavors:
        print(flavor)

class IceCreamStand(Restaurant):
"""餐馆里面卖冰淇淋"""

"""初始化餐馆"""
def __init__(self,restaurant_name,*cuisine_type):
    """初始化父类的属性"""
    super().__init__(restaurant_name,*cuisine_type)

    self.flavors=['草莓']
    self.show=Show(self.flavors)

icecream=IceCreamStand('冰淇淋店','冰淇淋')

icecream.flavors=['草莓','西瓜','香草','蓝莓']

print(icecream.flavors)

icecream.describe_restaurant()

icecream.open_restaurant()

icecream.show.show_flavors()

为什么修改flavors属性后直接打印显示修改成功了,但用将实例用作属性使用里面的方法列出来得到还是没有修改的值

The restaurant's name is 餐馆

The restaurant have the following cuisine_type ('食物',)

The 餐馆 is Open

['草莓', '西瓜', '香草', '蓝莓']

The restaurant's name is 冰淇淋店

The restaurant have the following cuisine_type ('冰淇淋',)

The 冰淇淋店 is Open

There are the following flavors:

草莓


  • 写回答

1条回答 默认 最新

  • 溪水人家 2019-10-10 19:57
    关注

    你的代码并没有改变Show里面的属性,而是改了IceCream里面的。Debug一下可以看到。
    图片说明

    改了一下你的代码

    class Restaurant():
        """餐馆"""
    
        def __init__(self,restaurant_name,*cuisine_type):
            """初始化餐换"""
    
            self.restaurant_name=restaurant_name
            self.cuisine_type=cuisine_type
    
        def describe_restaurant(self):
            """打印餐馆信息"""
            print("The restaurant's name is "+ self.restaurant_name)
            print("The restaurant have the following cuisine_type " + str(self.cuisine_type))
    
        def open_restaurant(self):
            """营业"""
            print("The "+self.restaurant_name+" is Open\n")
    
    
    restaurant=Restaurant('餐馆', '食物')
    restaurant.describe_restaurant()
    restaurant.open_restaurant()
    
    
    class Show():
        """列出列表"""
    
        def __init__(self, flavors):
            self.flavors = flavors
    
        def show_flavors(self):
            """列出口味"""
            print("There are the following flavors:")
            for flavor in self.flavors:
                print(flavor)
    
    
    class IceCreamStand(Restaurant):
        """餐馆里面卖冰淇淋"""
    
        """初始化餐馆"""
        def __init__(self, restaurant_name, *cuisine_type):
            """初始化父类的属性"""
            super().__init__(restaurant_name,*cuisine_type)
    
            self.flavors = ['草莓']
    
        def show_fla(self):
            show = Show(self.flavors)
            show.show_flavors()
    
    
    icecream = IceCreamStand('冰淇淋店', '冰淇淋')
    
    icecream.flavors = ['草莓','西瓜','香草','蓝莓']
    
    print(icecream.flavors)
    
    icecream.describe_restaurant()
    
    icecream.open_restaurant()
    
    icecream.show_fla()
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型