#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()
《Python编程从入门到实践》练习题代码实现第九章
最新推荐文章于 2024-07-14 22:18:32 发布