亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在一個屬性中存儲字符串列表

在一個屬性中存儲字符串列表

守著星空守著你 2023-02-15 16:46:50
我想我可能沒有清楚地解釋我的問題。我為此道歉。我會再試一次。我有一個具有某些屬性的父類:class Restaurant():'This is the restaurant class'def __init__(self, name, cuisine_type):    self.name = name    self.cuisine_type = cuisine_type然后我有一個子類繼承父類的所有屬性并添加一個新的:class IceCreamStand():def __init__(self, *flavor):    'Attributes of parent class initialised'    self.flavor = flavor現在我嘗試打印存儲在屬性 flavor 中的口味列表:def desc_flavor(self):    print('This Ice_Cream shop has ' + self.flavor + ' flavors')flavor1 = IceCreamStand('Mango', 'Raspberry', 'Coffee', 'Vanilla')如果我使用 concat,我會收到一條消息,指出名稱未定義。對于第一次沒有正確解釋問題,我深表歉意,并感謝您的所有幫助。
查看完整描述

4 回答

?
LEATH

TA貢獻1936條經驗 獲得超7個贊

class IceCreamStand(Restaurant):

      def __init__(self,restaurant_name, cuisine_type):

          super().__init__(restaurant_name, cuisine_type)

         

      def describe_flavors(self,*flavors):

          print(f'{self.restaurant_name} has the following flavors:')

          for self.flavor in flavors:

              print(f'-{self.flavor}')

           

restaurant =IceCreamStand('DQ','ice cream')

restaurant.describe_restaurant()

restaurant.describe_flavors('Chocolate','Mango', 'Raspberry', 'Coffee', 'Vanilla')


查看完整回答
反對 回復 2023-02-15
?
函數式編程

TA貢獻1807條經驗 獲得超9個贊

嘗試使用以下代碼:

def __init__(self, *attribute1):
    self.atributte1 = attribute1


查看完整回答
反對 回復 2023-02-15
?
aluckdog

TA貢獻1847條經驗 獲得超7個贊

使用任意參數列表??吹?a >這個答案。

例子:

li = []

def example(*arg):

    li = list(arg)

    print(li)


example('string1', 'string2', 'string3')


查看完整回答
反對 回復 2023-02-15
?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

據我所知,您正在做 Python 速成課程第 9 章的練習。這是我作為另一個練習的一部分所做的代碼。希望這對你有幫助。


class Restaurant():

"""A simple attempt to model a restaurant."""


    def __init__(self, restaurant_name, cusisine_type):

        self.name = restaurant_name

        self.type = cusisine_type


    def describe_restaurant(self):

        print("Restaurant name is " + self.name.title() + ".")

        # print(self.name.title() + " is a " + self.type + " type restaurant.")


    def open_restaurant(self):

        print(self.name.title() + " is open!")



class IceCreamStand(Restaurant):

"""Making a class that inherits from Restaurant parent class."""


    def __init__(self, restaurant_name, cusisine_type):

        super().__init__(restaurant_name, cusisine_type)

        self.flavor = 'chocolate'


    def display_flavors(self):

        print("This icrecream shop has " + self.flavor + " flavor.")



# create an instance of IceCreamStand

falvors = IceCreamStand('baskin robbins', 'icecream')


# print("My restaurant name is: " + falvors.name)

# print("Restaurant is which type: " + falvors.type)

falvors.describe_restaurant()

falvors.open_restaurant()


# Calling this method

falvors.display_flavors()


查看完整回答
反對 回復 2023-02-15
  • 4 回答
  • 0 關注
  • 134 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號