栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

《Python编程从入门到实践》练习题代码实现第九章

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

《Python编程从入门到实践》练习题代码实现第九章

#9-1 餐馆
class Restaurant: #创建类
    def __init__(self,restaurant_name,restaurant_type):#设置两个属性
        self.restaurant_name=restaurant_name
        self.restaurant_type=restaurant_type

    def describe_restaurant(self):
        print(f"the restaurant's name is {self.restaurant_name},"
              f"and its type is {self.restaurant_type}")
    def open_restaurant(self):
       print(f"the restaurant {self.restaurant_name} is opening")

restaurant=Restaurant('lucky','chinese')#创建名为restaurant的实例
print(restaurant.restaurant_name)
print(restaurant.restaurant_type)
restaurant.describe_restaurant()
restaurant.open_restaurant()

#9-2 三家餐馆
restaurant2=Restaurant('KFC','fast good')
restaurant2.describe_restaurant()
restaurant2.open_restaurant()
restaurant3=Restaurant('Starbucks','coffee')
restaurant3.describe_restaurant()
restaurant3.open_restaurant()

#9-3 用户
class User:
    def __init__(self,first_name,last_name,sex):
        self.first_name=first_name
        self.last_name=last_name
        self.sex=sex
    def describe_user(self):
        print(f"the user's first name is {self.first_name},"
              f"the last name is {self.last_name} and the sex is {self.sex}")
    def greet_user(self):
        print(f"hello!{self.first_name.title()} {self.last_name}")

user1=User('zhang','san','nan')
user2=User('ling','si','nv')
user1.describe_user()
user1.greet_user()
user2.describe_user()
user2.greet_user()

#9-4 就餐人数
class Restaurant: #创建类
    def __init__(self,restaurant_name,restaurant_type):#设置两个属性
        self.restaurant_name=restaurant_name
        self.restaurant_type=restaurant_type
        self.number_served=0
    def describe_restaurant(self):
        print(f"the restaurant's name is {self.restaurant_name},"
              f"and its type is {self.restaurant_type}")
    def open_restaurant(self):
        print(f"the restaurant {self.restaurant_name} is opening")
    def set_number_served(self,number):#设置就餐人数
        self.number_served=number
        print(f"here is {self.number_served} people")
    def increment_number_served(self,number):
        self.number_served=self.number_served+number
        print(f"here now is {self.number_served} people")
new_restaurant=Restaurant('bishengke','pizza')
print(f"here is {new_restaurant.number_served} people")
new_restaurant.number_served=20
print(f"here is {new_restaurant.number_served} people")
new_restaurant.set_number_served(30)
new_restaurant.increment_number_served(10)

#9-6 冰激凌小店
class IceCreamStand(Restaurant):
    """docstring for IceCreamStand"""
    def __init__(self, restaurant_name, cuisine_type, flavors):
        #Restaurant.__init__(restaurant_name, cuisine_type)
        super(IceCreamStand, self).__init__(restaurant_name, cuisine_type)
        self.flavors = flavors

Ice = IceCreamStand('ford','hot','cold')
print(Ice.flavors)
Ice.describe_restaurant()



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/754469.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号