《Python编程:从入门到实践》习题答案——第9章 类

本文介绍了Python中的类和对象实例,如Restaurant、User、Admin类的定义、方法和实例化,展示了如何创建餐厅对象、用户管理以及模块导入和使用,涉及数据结构(OrderedDict)和随机数生成(Dice)。
# 动手试一试
# 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):
        """定
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值