我正在尝试一种能够恢复冰淇淋摊的味道的方法。以下是我的代码:
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()
有什么想法吗?
TypeError:'str'对象不可调用
答案 0 :(得分:0)
是的,您使用同名属性隐藏方法。在您的__init__
self.flavors = flavors
中,您可以使用
my_IceCreamStand = IceCreamStand('West Ice Cream', 'Dessert', 'multiple')
现在,只要您的实例使用self.flavors
,它就会返回'multiple'
,这是一个字符串。然后尝试使用()
将其称为函数,如错误所示,字符串不可调用。
更改属性或方法的名称,但它们不应具有相同的名称。