#9-4 就餐人数:
代码:
# 9-4 就餐人数
class Restaurant():
"""define one restaurant class"""
def __init__(self, name, cuisine_type):
self.restaurant_name = name
self.cuisine_type = cuisine_type
self.number_served = 0
def describe_restaurant(self):
message = "This restaurant's name is: " + self.restaurant_name + "\n"
message += "The type of cuisine is: " + self.cuisine_type + "\n"
message += "Today it has served " + str(self.number_served) + "\n"
print(message)
def open_restaurant(self):
message = "The restuarant is running "
print(message)
def set_number_served(self, num):
self.number_served = num
def increment_number_served(self):
self.number_served += 1
restaurant = Restaurant('China Town', 'Chinese cuisine')
restaurant.describe_restaurant()
restaurant.open_restaurant()
restaurant.set_number_served(666)
restaurant.describe_restaurant()
restaurant.increment_number_served()
restaurant.describe_restaurant()
输出:
This restaurant's name is: China Town
The type of cuisine is: Chinese cuisine
Today it has served 0
The restuarant is running
This restaurant's name is: China Town
The type of cuisine is: Chinese cuisine
Today it has served 666
This restaurant's name is: China Town
The type of cuisine is: Chinese cuisine
Today it has served 667
#9-5 尝试登录次数:
代码:
#9-5 尝试登录次数
class User():
"""define a class called User, including name and other informations"""
def __init__(self, first_name, last_name, login_attempts, **info):
self.first_name = first_name
self.last_name = last_name
self.login_attempts = login_attempts
self.info = info
def describe_user(self):
message = "The user's name is " + self.first_name.title() + " " + self.last_name.title() + '\n'
for key, value in self.info.items():
message += key.title() + ": " + value.title() + '\n'
print(message)
def greet_user(self):
message = "Hello, Dear " + self.first_name.title() + " " + self.last_name.title()
print(message)
def increment_login_attempts(self):
self.login_attempts += 1
def reset_login_attempts(self):
self.login_attempts = 0
user = User("kobe", "branyt", 10, country = 'america', age = '38')
user.describe_user()
user.greet_user()
print(user.login_attempts)
for a in range(666):
user.increment_login_attempts()
print(user.login_attempts)
user.reset_login_attempts()
print(user.login_attempts)
输出:
The user's name is Kobe Branyt
Country: America
Age: 38
Hello, Dear Kobe Branyt
10
676
0
#9-6 冰淇淋小店:
代码:
#9-6 冰淇淋小店
class Restaurant():
"""define one restaurant class"""
def __init__(self, name, cuisine_type):
self.restaurant_name = name
self.cuisine_type = cuisine_type
self.number_served = 0
def describe_restaurant(self):
message = "This restaurant's name is: " + self.restaurant_name + "\n"
message += "The type of cuisine is: " + self.cuisine_type + "\n"
message += "Today it has served " + str(self.number_served) + "\n"
print(message)
def open_restaurant(self):
message = "The restuarant is running\n"
print(message)
def set_number_served(self, num):
self.number_served = num
def increment_number_served(self):
self.number_served += 1
class IceCreamStand(Restaurant):
"""inherit the class Restaurant"""
def __init__(self, name, cuisine_type, flavors):
super().__init__(name, cuisine_type)
self.flavors = flavors
def show_IceCream_flavors(self):
message = self.restaurant_name + " have different " + self.cuisine_type + ": "
print(message)
for value in self.flavors:
print(value)
flavors = ['Coffee and Cookie ice cream', 'Chocolate ice cream', 'Cherry ice cream']
Stand = IceCreamStand('Ice Cream Stand', 'Ice Cream', flavors)
Stand.describe_restaurant()
Stand.open_restaurant()
Stand.show_IceCream_flavors()
输出:
This restaurant's name is: Ice Cream Stand
The type of cuisine is: Ice Cream
Today it has served 0
The restuarant is running
Ice Cream Stand have different Ice Cream:
Coffee and Cookie ice cream
Chocolate ice cream
Cherry ice cream
#9-7 管理员:
代码:
#9-7 管理员
class User():
"""define a class called User, including name and other informations"""
def __init__(self, first_name, last_name, login_attempts, **info):
self.first_name = first_name
self.last_name = last_name
self.login_attempts = login_attempts
self.info = info
def describe_user(self):
message = "The user's name is " + self.first_name.title() + " " + self.last_name.title() + '\n'
for key, value in self.info.items():
message += key.title() + ": " + value.title() + '\n'
print(message)
def greet_user(self):
message = "Hello, Dear " + self.first_name.title() + " " + self.last_name.title()
print(message)
def increment_login_attempts(self):
self.login_attempts += 1
def reset_login_attempts(self):
self.login_attempts = 0
class Admin(User):
"""define a special user, Admin"""
def __init__(self, first_name, last_name, login_attempts, privileges, **info):
super().__init__(first_name, last_name, login_attempts, **info)
self.privileges = privileges
def show_privileges(self):
message = "You " + self.privileges
print(message)
admin = Admin("kobe", "branyt", 10, "can do everything", country = 'america', age = '38')
admin.describe_user()
admin.greet_user()
admin.show_privileges()
输出:
The user's name is Kobe Branyt
Country: America
Age: 38
Hello, Dear Kobe Branyt
You can do everything
#9-8 权限:
代码:
#9-8 权限
class User():
"""define a class called User, including name and other informations"""
def __init__(self, first_name, last_name, login_attempts, **info):
self.first_name = first_name
self.last_name = last_name
self.login_attempts = login_attempts
self.info = info
def describe_user(self):
message = "The user's name is " + self.first_name.title() + " " + self.last_name.title() + '\n'
for key, value in self.info.items():
message += key.title() + ": " + value.title() + '\n'
print(message)
def greet_user(self):
message = "Hello, Dear " + self.first_name.title() + " " + self.last_name.title()
print(message)
def increment_login_attempts(self):
self.login_attempts += 1
def reset_login_attempts(self):
self.login_attempts = 0
class Admin(User):
"""define a special user, Admin"""
def __init__(self, first_name, last_name, login_attempts, privileges, **info):
super().__init__(first_name, last_name, login_attempts, **info)
self.privileges = Privileges(privileges)
def show_privileges(self):
message = "You " + self.privileges.privileges
print(message)
class Privileges():
"""define the property of Admin"""
def __init__(self, privileges):
self.privileges = privileges
admin = Admin("kobe", "branyt", 10, "can do everything", country = 'america', age = '38')
admin.describe_user()
admin.greet_user()
admin.show_privileges()
输出:
The user's name is Kobe Branyt
Country: America
Age: 38
Hello, Dear Kobe Branyt
You can do everything