Q值。做一个名为餐厅的课程。 Restaurant的 init ()方法应存储两个属性:restaurant_name和cuisine_type。创建一个名为describe_restaurant()的方法来打印这两条信息,以及一个名为open_restaurant()的方法,它打印一条消息,表明餐厅是开放的。 从您的班级创建一个名为restaurant的实例。单独打印两个属性,然后调用这两种方法。
big chill
italian
Traceback (most recent call last):
File "/Users/rishabhchopra/Desktop/practice2/9-1.Restaurant.py", line 21, in <module>
restaurant1.describe_restaurant()
AttributeError: 'Restaurant' object has no attribute 'describe_restaurant'
[Finished in 0.1s with exit code 1]
我得到的输出为:
<script>
var phpParams = <?php echo json_encode($jsParams); ?>;
</script>
问:为什么Python会将我的类的方法解释为属性?
答案 0 :(得分:1)
假设你的缩进真的像这样
class Restaurant():
"""An attempt to model a restaurant"""
def __init__(self,restaurant_name,cuisine_type):
"""Initializing name and age attributes"""
self.name = restaurant_name
self.cuisine = cuisine_type
def describe_restaurant(self):
# Describes the restaurant name and cuisine
print(self.name.title() + " serves " + self.cuisine + " food.")
def open_restaurant(self):
# Opens the restaurant and welcomes customers.
print(self.name.title() + " is now Open. \nCome. \nHave some delicious " + self.cuisine + "
您在__init__
方法中意外嵌套了所需的方法,使其无法访问。修复嵌套,你不应该有任何问题。
class Restaurant():
"""An attempt to model a restaurant"""
def __init__(self,restaurant_name,cuisine_type):
"""Initializing name and age attributes"""
self.name = restaurant_name
self.cuisine = cuisine_type
def describe_restaurant(self):
# Describes the restaurant name and cuisine
print(self.name.title() + " serves " + self.cuisine + " food.")
def open_restaurant(self):
# Opens the restaurant and welcomes customers.
print(self.name.title() + " is now Open. \nCome. \nHave some delicious " + self.cuisine + "
请注意,方法是cdarke提到的属性 - 错误消息非常清楚。
答案 1 :(得分:0)
纠正你的缩进,它工作正常:
class Restaurant():
"""An attempt to model a restaurant"""
def __init__(self,restaurant_name,cuisine_type):
"""Initializing name and age attributes"""
self.name = restaurant_name
self.cuisine = cuisine_type
def describe_restaurant(self):
# Describes the restaurant name and cuisine
print(self.name.title() + " serves " + self.cuisine + " food.")
def open_restaurant(self):
# Opens the restaurant and welcomes customers.
print(self.name.title() + " is now Open. \nCome. \nHave some delicious " + self.cuisine + " food.")
restaurant1 = Restaurant("big chill","italian")
print(restaurant1.name)
print(restaurant1.cuisine)
restaurant1.describe_restaurant()
顺便说一下,方法是属性。
答案 2 :(得分:0)
它只是缩进。
class Restaurant():
def __init__(self,restaurant_name,cuisine_type):
self.name = restaurant_name
self.cuisine = cuisine_type
def describe_restaurant(self):
# Describes the restaurant name and cuisine
print(self.name.title() + " serves " + self.cuisine + " food.")
def open_restaurant(self):
# Opens the restaurant and welcomes customers.
print(self.name.title() + " is now Open. \nCome. \nHave some delicious " + self.cuisine + " food.")
restaurant1 = Restaurant("big chill","italian")
print(restaurant1.name)
print(restaurant1.cuisine)
restaurant1.describe_restaurant()