"""餐馆:创建一个名为restaurant的类,其方法__init__()设置两个属性restaurant_name
和cuisine_type.创建一个名为describe_restaurant()的方法一个名为open_restaurant()的方法
其中前者打印前述两项信息,而后者打印一条消息,支出餐馆正在营业"""
print("9-1编写类")
class restaurant():
"""模拟餐馆的信息"""
"""__init__是一个特殊的方法,开头和末尾使用下划线的是一种约定"""
def __init__(self,restaurant_name,cuisine_type): #self实现自动传递实参给实例
"""初始化属性restaurant_name 和cuisine_type"""
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
"""描述餐馆名字"""
print("\nThis restaurant name is: " + self.restaurant_name)
print("This type is " + self.cuisine_type )
def open_restaurant(self):
"""餐馆正在营业"""
print("The restaurant is opening now")
print("9-2三个实例")
"""实例化和调用方法"""
my_restaurant = restaurant('北京酒家','fn')
my_restaurant.describe_restaurant()
my_restaurant.open_restaurant()
"""实例化和调用方法"""
your_restaurant = restaurant('广州酒家','yang')
your_restaurant.describe_restaurant()
your_restaurant.open_restaurant()
"""实例化和调用方法"""
our_restaurant = restaurant('惠州酒家','meng')
our_restaurant.describe_restaurant()
our_restaurant.open_restaurant()
print("\nMy restaurant name is: " + my_restaurant.restaurant_name.title())
print("My restaurant type is: " + my_restaurant.cuisine_type)
print("\nYour restaurant name is: " + your_restaurant.restaurant_name.title())
print("Your restaurant type is: " + your_restaurant.cuisine_type)
print("\nOur restaurant name is: " + our_restaurant.restaurant_name.title())
print("Our restaurant type is: " + our_restaurant.cuisine_type)
"""9-3创建User类"""
class User():
"""初始化属性first_name,last_name"""
def __init__(self,first_name,last_name):
self.first_name = first_name
self.last_name = last_name
"""定义方法"""
def describe_user(self):
print("Your name is: " + self.first_name.title() + self.last_name.title())
def greet_user(self):
print("Hello!" + self.first_name.title() + self.last_name.title() )
"""实例化"""
my_name = User("cong","meng")
"""调用方法"""
my_name.greet_user()
my_name.describe_user()
print("My name is:" + my_name.first_name.title(),my_name.last_name.title())
"""9-4餐馆:创建一个名为restaurant的类,其方法__init__()设置两个属性restaurant_name
和cuisine_type.创建一个名为describe_restaurant()的方法一个名为open_restaurant()的方法
其中前者打印前述两项信息,而后者打印一条消息,支出餐馆正在营业"""
print("9-1编写类")
class restaurant():
"""模拟餐馆的信息"""
"""__init__是一个特殊的方法,开头和末尾使用下划线的是一种约定"""
def __init__(self,restaurant_name,cuisine_type): #self实现自动传递实参给实例
"""初始化属性restaurant_name 和cuisine_type"""
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
self.number_served = 0
def set_number_served(self):
print("how many people? " + str(self.number_served))
def describe_restaurant(self):
"""描述餐馆名字"""
print("\nThis restaurant name is: " + self.restaurant_name)
print("This type is " + self.cuisine_type )
print("How many people have danner in the restaurant? " + str(self.number_served))
def open_restaurant(self):
"""餐馆正在营业"""
print("The restaurant is opening now")
def increment_number_served(self, numbers):
self.number_served += numbers
"""实例化"""
his_restaurant = restaurant('上海酒家','hotel')
his_restaurant.set_number_served()
his_restaurant.describe_restaurant()
"""1.直接修改属性的值"""
his_restaurant.number_served = 23
his_restaurant.set_number_served()
"""递增就餐人数"""
#she_restaurant = restaurant('杭州酒家','ho')
his_restaurant.increment_number_served(10)
his_restaurant.set_number_served()
"""9-5创建User类"""
class User():
"""初始化属性first_name,last_name"""
def __init__(self,first_name,last_name):
self.first_name = first_name
self.last_name = last_name
self.login_attempts = 0
"""定义方法"""
def describe_user(self):
print("Your name is: " + self.first_name.title() + self.last_name.title())
def greet_user(self):
print("Hello!" + self.first_name.title() + self.last_name.title() )
def read_login_attempts(self):
print("This login attempts is: " + str(self.login_attempts))
"""将属性值 +1 """
def increment_login_attempts(self):
self.login_attempts += 1
"""重置属性值"""
def reset_login_attemptes(self):
self.login_attempts = 0
"""实例化"""
my_name = User("cong","meng")
"""调用方法"""
my_name.greet_user()
my_name.describe_user()
print("My name is:" + my_name.first_name.title(),my_name.last_name.title())
"""调用increment_login_attempts方法"""
your_name = User('bobo','bo')
your_name.read_login_attempts()
your_name.increment_login_attempts()
your_name.read_login_attempts()
"""多次调用"""
your_name.increment_login_attempts()
your_name.increment_login_attempts()
#your_name.reset_login_attemptes()
your_name.read_login_attempts()
your_name.reset_login_attemptes()
your_name.read_login_attempts()
print("9-6冰淇淋小店")
class restaurant():
"""模拟餐馆的信息"""
"""__init__是一个特殊的方法,开头和末尾使用下划线的是一种约定"""
def __init__(self, restaurant_name, cuisine_type): # self实现自动传递实参给实例
"""初始化属性restaurant_name 和cuisine_type"""
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
"""描述餐馆名字"""
print("\nThis restaurant name is: " + self.restaurant_name)
print("This type is " + self.cuisine_type)
def open_restaurant(self):
"""餐馆正在营业"""
print("The restaurant is opening now")
class IceCreamStand(restaurant):
def __init__(self,restaurant_name,cuisine_type):
super().__init__(restaurant_name,cuisine_type)
self.flavors = ['草莓味','香蕉味','苹果味']
def describe_icecreamstand(self):
for flavor in self.flavors:
print("this iceCreamStand is: " + flavor)
my_restaurant = IceCreamStand('北京酒家','fn')
my_restaurant.describe_restaurant()
my_restaurant.open_restaurant()
my_restaurant.describe_icecreamstand()
print('9-7')
#管理员:管理员是一种特殊的用户,编写一个名为Admin的类,让他继承你 为完成9-3
#或练习9-5而编写的User类。添加一个名为privileges的属性,用户存储一个由字符串(“
#如“can add post()的方法、”can deletepost“,”canban user "等—
#"""创建User类"""
class User():
"""初始化属性first_name,last_name"""
def __init__(self,first_name,last_name):
self.first_name = first_name
self.last_name = last_name
"""定义方法"""
def describe_user(self):
print("Your name is: " + self.first_name.title() + self.last_name.title())
def greet_user(self):
print("Hello!" + self.first_name.title() + self.last_name.title() )
class Admin(User):
"""添加特殊用户管理员admain"""
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):
print("This admin has: " + str(self.privileges))
#实例化
my_user = Admin('yang','bo')
my_user.show_privileges()
print('9-7')
#管理员:管理员是一种特殊的用户,编写一个名为Admin的类,让他继承你 为完成9-3
#或练习9-5而编写的User类。添加一个名为privileges的属性,用户存储一个由字符串(“
#如“can add post()的方法、”can deletepost“,”canban user "等—
#"""创建User类"""
class User():
"""初始化属性first_name,last_name"""
def __init__(self,first_name,last_name):
self.first_name = first_name
self.last_name = last_name
"""定义方法"""
def describe_user(self):
print("Your name is: " + self.first_name.title() + self.last_name.title())
def greet_user(self):
print("Hello!" + self.first_name.title() + self.last_name.title() )
class Admin(User):
"""添加特殊用户管理员admain"""
def __init__(self,first_name,last_name):
super().__init__(first_name,last_name)
self.privileges = privileges()
#9-8权限
class privileges():
def __init__(self):
self.privileges = ['can add post', 'can delete post', 'can ban user']
def show_privileges(self):
print("This admin is:" + str(self.privileges))
#实例化
my_user = Admin('yang','bo')
my_user.privileges.show_privileges()