9-1 餐馆
class Restaurant():
def __init__(self, restaurant_name, cuisine_type):
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
print(
self.restaurant_name + " main " + self.cuisine_type + ".")
def open_restaurant(self):
print("The restaurant is opening.")
re = Restaurant('Dasanyuan', 'Cantonese food')
print(re.restaurant_name + " " + re.cuisine_type)
re.describe_restaurant()
re.open_restaurant()
输出:
Dasanyuan Cantonese food
Dasanyuan main Cantonese food.
The restaurant is opening.
9-2 三家餐馆
class Restaurant():
def __init__(self, restaurant_name, cuisine_type):
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
print(
self.restaurant_name + " main " + self.cuisine_type + ".")
def open_restaurant(self):
print("The restaurant is opening.")
re1 = Restaurant('Dasanyuan', 'Cantonese food')
re2 = Restaurant('White Swan', 'Western-style food')
re3 = Restaurant('Chongqing chicken soup', 'Hot Pot')
re1.describe_restaurant()
re2.describe_restaurant()
re3.describe_restaurant()
输出:
Dasanyuan main Cantonese food.
White Swan main Western-style food.
Chongqing chicken soup main Hot Pot.
9-3 用户
class User():
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name
self.age = 23
def describe_user(self):
print("User name: " + self.first_name + " " + self.last_name)
print("User age: " + str(self.age))
def greet_user(self):
print(
"welcome to the party, " + self.first_name + " " + self.last_name)
user1 = User("Li", "Hua")
user2 = User("Steven", "Zhou")
user1.describe_user()
user1.greet_user()
user2.describe_user()
user2.greet_user()
输出:
User name: Li Hua
User age: 23
welcome to the party, Li Hua
User name: Steven Zhou
User age: 23
welcome to the party, Steven Zhou
9-4 就餐人数
class Restaurant():
def __init__(self, restaurant_name, cuisine_type):
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
self.number_served = 0
def describe_restaurant(self):
print(self.restaurant_name + " main " + self.cuisine_type + ".")
def open_restaurant(self):
print("The restaurant is opening.")
def set_number_served(self, num):
self.number_served = num
def increment_number_served(self, num):
self.number_served += num
restaurant = Restaurant('Dasanyuan', 'Cantonese food')
print(
str(restaurant.number_served) + " people have eaten in " +
restaurant.restaurant_name + ".")
restaurant.number_served = 12
print(
str(restaurant.number_served) + " people have eaten in " +
restaurant.restaurant_name + ".")
restaurant.set_number_served(34)
print(
str(restaurant.number_served) + " people have eaten in " +
restaurant.restaurant_name + ".")
restaurant.increment_number_served(10)
print(
str(restaurant.number_served) + " people have eaten in " +
restaurant.restaurant_name + ".")
输出:
0 people have eaten in Dasanyuan.
12 people have eaten in Dasanyuan.
34 people have eaten in Dasanyuan.
44 people have eaten in Dasanyuan.
9-5 尝试登录次数
class User():
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name
self.age = 23
self.login_attempts = 0
def increment_login_attempts(self):
self.login_attempts += 1
def reset_login_attempts(self):
self.login_attempts = 0
def describe_user(self):
print("User name: " + self.first_name + " " + self.last_name)
print("User age: " + str(self.age))
def greet_user(self):
print(
"welcome to the party, " + self.first_name + " " + self.last_name)
user1 = User("Li", "Hua")
print(user1.login_attempts)
for i in range(5):
user1.increment_login_attempts()
print(user1.login_attempts)
user1.reset_login_attempts()
print(user1.login_attempts)
输出:
0
1
2
3
4
5
0
9-6 冰淇淋小店
class Restaurant():
def __init__(self, restaurant_name, cuisine_type):
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
self.number_served = 0
def describe_restaurant(self):
print(self.restaurant_name + " main " + self.cuisine_type + ".")
def open_restaurant(self):
print("The restaurant is opening.")
def set_number_served(self, num):
self.number_served = num
def increment_number_served(self, num):
self.number_served += num
class IceCreamStand(Restaurant):
def __init__(self, restaurant_name, cuisine_type):
super().__init__(restaurant_name, cuisine_type)
self.flavors = [
"ice cream", "frozen yogurt", "Fruit and vegetable ice cream",
"Chocolate Ice Cream"
]
def describe_flavors(self):
for flavor in self.flavors:
print(flavor)
ice = IceCreamStand("Ice cream shop", "Ice cream")
ice.describe_flavors()
输出:
ice cream
frozen yogurt
Fruit and vegetable ice cream
Chocolate Ice Cream
9-7 管理员
class User():
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name
self.age = 23
self.login_attempts = 0
def increment_login_attempts(self):
self.login_attempts += 1
def reset_login_attempts(self):
self.login_attempts = 0
def describe_user(self):
print("User name: " + self.first_name + " " + self.last_name)
print("User age: " + str(self.age))
def greet_user(self):
print(
"welcome to the party, " + self.first_name + " " + self.last_name)
class Admin(User):
def __init__(self, first_name, last_name):
super().__init__(first_name, last_name)
self.privileges = [
'can add post',
'can delete post',
'can ban user'
]
def show_privileges(self):
for privilege in self.privileges:
print(privilege)
user = Admin('Li', 'Hua')
user.show_privileges()
输出:
can add post
can delete post
can ban user
9-8 权限
class User():
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name
self.age = 23
self.login_attempts = 0
def increment_login_attempts(self):
self.login_attempts += 1
def reset_login_attempts(self):
self.login_attempts = 0
def describe_user(self):
print("User name: " + self.first_name + " " + self.last_name)
print("User age: " + str(self.age))
def greet_user(self):
print(
"welcome to the party, " + self.first_name + " " + self.last_name)
class Privileges():
def __init__(self):
self.privileges = ['can add post', 'can delete post', 'can ban user']
def show_privileges(self):
for privilege in self.privileges:
print(privilege)
class Admin(User):
def __init__(self, first_name, last_name):
super().__init__(first_name, last_name)
self.privileges = Privileges()
user = Admin('Li', 'Hua')
user.privileges.show_privileges()
输出:
can add post
can delete post
can ban user