[英]Child class cant find the attribute I created
每次我運行我的代碼時,我都會收到此錯誤“AttributeError: 'IceCreamStand' object has no attribute 'flavors'”。 我正在嘗試為我的孩子 class 設置一個新屬性,這就是我編寫 self.flavors 的原因,它似乎沒有為我的孩子 class 創建一個新屬性。
為什么這不起作用? 提前致謝。
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 name of the restaurant is {self.restaurant_name}")
print(f"The cuisine type of the restaurant is {self.cuisine_type}")
def open_restaurant(self):
print(f"The {self.restaurant_name} is open")
class IceCreamStand(Restaurant):
def __intit__(self, restaurant_name, cuisine_type):
super().__intit__(restaurant_name, cuisine_type)
self.flavors = ["Strawberry", "Cheese", "Chocolate"]
def display_flavors(self):
print(f"These are the flavors: {self.flavors}")
Icecream = IceCreamStand("Northgate Icecream", "Mocha icecream")
Icecream.display_flavors()
將def __intit__
更改為def __init__
。
您不小心將init命名為init
如果將構造函數名稱更正為init
class IceCreamStand(Restaurant):
def __init__(self, restaurant_name, cuisine_type):
super().__init__(restaurant_name, cuisine_type)
self.flavors = ["Strawberry", "Cheese", "Chocolate"]
def display_flavors(self):
print(f"These are the flavors: {self.flavors}")
然后它可以正常工作!
干杯!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.