coding=gbk
coding:utf-8!
chapter 9 practice
9-1餐馆
class Restaurant():
“”“简单创建一个名为restaurant的类”""
def init(self, name, type):
“”“设置name和type两个属性”""
self.name = name
self.type = type
def describe(self):
print(“The restaurant’s name is " + self.name.title()
+ " and it has " + self.type + " cuisine.”)
def open(self):
print(“The restaurant " + self.name.title()
+ " is now opening.”)
restaurant_1 = Restaurant(‘nayuki’, ‘western’) # 别忘了:实例=类()
restaurant_1.describe() # 注意这里实例与方法的位置:实例.方法
restaurant_1.open()
网上参考答案
print(“网上参考答案”)
class Restaurant():
def init(self,restaurant_name,cuisine_type):
“”“初始化属性restaurant_name和cuisine_type”""
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
print(self.restaurant_name)
print(self.cuisine_type)
def open_restaurant(self):
print(‘Restaurant is open’)
restaurant = Restaurant(‘A’,‘B’)
print(restaurant.restaurant_name,’’,restaurant.cuisine_type)
restaurant.describe_restaurant()
restaurant.open_restaurant()
9-2三家餐馆
print("\n三家餐馆")
class Restaurant():
“”“简单创建一个名为restaurant的类”""
def init(self, name, type):
“”“设置name和type两个属性”""
self.name = name
self.type = type
def describe(self):
print(“The restaurant’s name is " + self.name.title()
+ " and it has " + self.type + " cuisine.”)
def open(self):
print(“The restaurant " + self.name.title()
+ " is now opening.”)
restaurant_1 = Restaurant(‘nayuki’, ‘Western’)
restaurant_2 = Restaurant(‘papa john’, ‘Italian’)
restaurant_3 = Restaurant(‘taco’, ‘Spanish’)
restaurant_1.describe()
restaurant_1.open()
restaurant_2.describe()
restaurant_2.open()
restaurant_3.describe()
restaurant_3.open()
9-3用户
print("\n用户")
class User():
def init(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name
def describe(self):
print("First name: " + self.first_name.title())
print("Last name: " + self.last_name.title())
def great_user(self):
print("Hello " + self.first_name.title() + " "
+ self.last_name.title() + “.”)
user_1 = User(‘tina’, ‘wang’)
user_2 = User(‘john’, ‘hu’)
user_3 = User(‘joseph’, ‘hu’)
user_1.describe()
user_1.great_user()
user_2.describe()
user_2.great_user()
user_3.describe()
user_3.great_user()