# 动手试一试
# 9-1 餐厅
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.title() + " is opening.")
print(self.restaurant_name.title() + "'s food is " + self.cuisine_type)
def open_restaurant(self):
"""餐厅营业时间"""
print(self.restaurant_name + " is opening at 10:00~22:00.")
good = Restaurant('tilly', 'chinese')
print(good.restaurant_name.title())
print(good.cuisine_type.title())
good.describe_restaurant()
good.open_restaurant()
# 9-2 三家餐厅:重新创建三个实例
print("\n")
happy = Restaurant('hili', 'france')
print(happy.restaurant_name.title())
print(happy.cuisine_type.title())
happy.describe_restaurant()
happy.open_restaurant()
print("\n")
nice = Restaurant('cidy', 'american')
print(nice.restaurant_name.title())
print(nice.cuisine_type.title())
nice.describe_restaurant()
nice.open_restaurant()
# 9-3 用户:有问题
class User():
def __init__(self, first_name, last_name, tel, address):
self.first_name = first_name
self.last_name = last_name
self.tel = tel
self.address = address
def describe_user(self):
print(self.first_name + self.last_name + "的电话号码是:" + self.tel)
print("他(她)的住址是:" + self.address)
def greet_user(self):
print(self.first_name + self.last_name + "您好,欢迎您来到安徽合肥!")
wang = User('王','三','124567899','上海')
wang.describe_user()
wang.greet_user()
print("\n")
zhang = User('张','五','4343555555','北京')
zhang.describe_user()
zhang.greet_user()
print("\n")
huang = User('黄','八','12308','香港')
huang.describe_user()
huang.greet_user()
# 9-4 就餐人数
class Restaurant():
def __init__(self, restaurant_name, cuisine_type):
"""初始化属性restaurant_name和cuisine_type"""
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
self.number_served = 0
def person(self):
"""定
《Python编程:从入门到实践》习题答案——第9章 类
最新推荐文章于 2023-09-07 15:36:26 发布
本文介绍了Python中的类和对象实例,如Restaurant、User、Admin类的定义、方法和实例化,展示了如何创建餐厅对象、用户管理以及模块导入和使用,涉及数据结构(OrderedDict)和随机数生成(Dice)。

最低0.47元/天 解锁文章
577

被折叠的 条评论
为什么被折叠?



