我是新來的類,我想在我的「Python速成課程」書中完成練習9-1,問題的最後部分要求我回電我的方法,但我最終得到調用一個函數,沒有定義的錯誤
'未定義的錯誤'爲
describe_restaurant()
。
這裏是我的代碼:
class Restaurant():
def __init__(self, r_name, c_type):
self.r_name = r_name
self.c_type = c_type
def describe_restaurant():
print(self.r_name.title())
print(self.c_type.title())
def open_restaurant():
print(self.r_name + " is now open!")
Restaurant = Restaurant('Joe\'s Sushi', 'sushi')
print(Restaurant.r_name)
print(Restaurant.c_type)
describe_restaurant()
open_restaurant()
我認爲describe_restaurant
應該不需要,因爲我呼喚它作爲使用功能雖然被定義?
您應該首先創建'Restaraunt'對象,然後從新創建的對象調用'describe_restaurant' –
這是一個類函數。您需要使用類對象調用類函數。 – MooingRawr
你應該重新審視類的課程計劃。下面是通過官方教程的另一種看法:https://docs.python.org/3/tutorial/classes.html – idjaw