2017-02-25 159 views
0

我想調用一個方法,將返回冰淇淋攤的味道。下面是我的代碼:Python的繼承和調用方法

class Restaurant: 
    """Defining Restaurants""" 
    def __init__ (self, restaurant_name, cuisine_type): 

     """Initializing a restaurant name and cuisine type""" 
     self.restaurant_name = restaurant_name 
     self.cuisine_type = cuisine_type 
     self.number_served = 545 

    def describe_restaurant(self): 
     """Describing the restaurant""" 
     print(self.restaurant_name.title() + " serves " + self.cuisine_type.title() + ".") 

    def open_restaurant(self): 
     """Stating the restaurant is open""" 
     print(self.restaurant_name.title() + " is now open.") 

    def set_number_served(self): 
     """Method that sets the number served""" 
     set_number_served = 430 
     print("This restaurant has served " + str(set_number_served) + " customers.") 

    def restaurant(self): 
     """Statement showing how many customers have been served""" 
     print("This restaurant has served " + str(self.number_served) + " customers.") 

    def increment_number_served(self): 
     self.number_served += 432 
     print("This restaurant has served " + str(self.number_served) + " customers.") 

class IceCreamStand(Restaurant): 
    """Represents an ice cream stand""" 
    def __init__(self, restaurant_name, cuisine_type, flavors): 
     self.restaurant_name = restaurant_name 
     self.cuisine_type = cuisine_type 
     self.flavors = flavors 

    def flavors(self): 
     """Flavors of ice cream""" 
     flavors = ['chocolate', 'vanilla', 'strawberry'] 
     for flavor in flavors: 
      return flavor.title() 

my_IceCreamStand = IceCreamStand('West Ice Cream', 'Dessert', 'multiple') 
my_IceCreamStand.flavors() 

我收到的錯誤是:

Traceback (most recent call last): 
    File "ice_Cream_Stand.py", line 45, in <module> 
    my_IceCreamStand.flavors() 

任何想法?

類型錯誤:「海峽」對象不是可調用

回答

0

是的,你用相同名稱的屬性影子你的方法。在你__init__你做self.flavors = flavors和你所以現在

my_IceCreamStand = IceCreamStand('West Ice Cream', 'Dessert', 'multiple') 

初始化,您的實例使用self.flavors任何時候,它將返回'multiple',這是一個字符串。然後嘗試使用()將它稱爲函數,並且如錯誤所述,字符串不可調用。

更改屬性或方法的名稱,但它們不應該具有相同的名稱。

+0

太棒了,就是這樣!謝謝你! –