class Restaurant:def __init__(self,restaurant_name,cuisine_type):self.restaurant_name=restaurant_nameself.cuisine_type=cuisine_typedef describe_restaurant(self):print(f"restaurant_name:{self.restaurant_name}")print(f"cuisine_type:{self.cuisine_type}")def open_restaurant(self):print("餐馆正在营业")restaurant=Restaurant('A','a')
restaurant.describe_restaurant()
restaurant.open_restaurant()

class Restaurant:def __init__(self,restaurant_name,cuisine_type):self.restaurant_name=restaurant_nameself.cuisine_type=cuisine_typedef describe_restaurant(self):print(f"restaurant_name:{self.restaurant_name}")print(f"cuisine_type:{self.cuisine_type}")def open_restaurant(self):print("餐馆正在营业")a=Restaurant('A','a')
b=Restaurant('B','b')
c=Restaurant('C','c')
a.describe_restaurant()
b.describe_restaurant()
c.describe_restaurant()

创建多个表示不同用户的实例, 并对每个实例都调用上述两个方法
class User:def __init__(self,first_name,last_name):self.first_name=first_nameself.last_name=last_namedef describe_user(self):print(self.first_name)print(self.last_name)def greet_user(self):print("Hello~")a=User('A','a')
a.describe_user()
a.greet_user()

根据这个类创建一个名为restaurant 的实例; 打印有多少人在这家餐馆就餐过, 然后修改这个值并再次打印它。
添加一个名为set_number_served() 的方法, 它让你能够设置就餐人数。 调用这个方法并向它传递一个值, 然后再次打印这个值。
添加一个名为increment_number_served() 的方法, 它让你能够将就餐人数递增。 调用这个方法并向它传递一个这样的值:你认为这家餐馆每天可能接待的就餐人数。
1、根据这个类创建一个名为restaurant 的实例; 打印有多少人在这家餐馆就餐过
class Restaurant:def __init__(self,restaurant_name,cuisine_type):self.restaurant_name=restaurant_nameself.cuisine_type=cuisine_typeself.number_served=0def describe_restaurant(self):print(f"restaurant_name:{self.restaurant_name}")print(f"cuisine_type:{self.cuisine_type}")def open_restaurant(self):print("餐馆正在营业")
restaurant=Restaurant('A','a')
print(f"多少人在这家餐馆就餐过:{restaurant.number_served}")

2、根据这个类创建一个名为restaurant 的实例,修改这个值并再次打印它。
class Restaurant:def __init__(self,restaurant_name,cuisine_type):self.restaurant_name=restaurant_nameself.cuisine_type=cuisine_typeself.number_served=0def describe_restaurant(self):print(f"restaurant_name:{self.restaurant_name}")print(f"cuisine_type:{self.cuisine_type}")def open_restaurant(self):print("餐馆正在营业")
restaurant=Restaurant('A','a')
restaurant.number_served=10
print(f"多少人在这家餐馆就餐过:{restaurant.number_served}")

3、添加一个名为set_number_served() 的方法, 它让你能够设置就餐人数。 调用这个方法并向它传递一个值, 然后再次打印这个值。
class Restaurant:def __init__(self,restaurant_name,cuisine_type):self.restaurant_name=restaurant_nameself.cuisine_type=cuisine_typeself.number_served=0def describe_restaurant(self):print(f"restaurant_name:{self.restaurant_name}")print(f"cuisine_type:{self.cuisine_type}")def open_restaurant(self):print("餐馆正在营业")def set_number_served(self,number_served):self.number_served=number_servedrestaurant=Restaurant('A','a')
restaurant.set_number_served(11)
print(f"多少人在这家餐馆就餐过:{restaurant.number_served}")

4、添加一个名为increment_number_served() 的方法, 它让你能够将就餐人数递增。调用这个方法并向它传递一个这样的值:你认为这家餐馆每天可能接待的就餐人数。
class Restaurant:def __init__(self,restaurant_name,cuisine_type):self.restaurant_name=restaurant_nameself.cuisine_type=cuisine_typeself.number_served=0def describe_restaurant(self):print(f"restaurant_name:{self.restaurant_name}")print(f"cuisine_type:{self.cuisine_type}")def open_restaurant(self):print("餐馆正在营业")def set_number_served(self,number_served):self.number_served = number_serveddef increment_number_served(self,digit):self.number_served += digitrestaurant=Restaurant('A','a')
restaurant.set_number_served(11)
print(f"多少人在这家餐馆就餐过:{restaurant.number_served}")
restaurant.increment_number_served(10)
print(f"多少人在这家餐馆就餐过:{restaurant.number_served}")

编写一个名为increment_login_attempts() 的方法,它将属性login_attempts 的值加1。
再编写一个名为reset_login_attempts() 的方法, 它将属性login_attempts 的值重置为0。
根据User类创建一个实例, 再调用方法increment_login_attempts() 多次。
打印属性login_attempts 的值,确认它被正确地递增;
然后, 调用方法reset_login_attempts() , 并再次打印属性login_attempts 的值, 确认它被重置为0。
class User:def __init__(self,first_name,last_name):self.first_name=first_nameself.last_name=last_nameself.login_attempts=0def describe_user(self):print(self.first_name)print(self.last_name)def greet_user(self):print("Hello~")def increment_login_attempts(self):self.login_attempts+=1def reset_login_attempts(self):self.login_attempts=0user=User('A','a')
user.increment_login_attempts()
user.increment_login_attempts()
user.increment_login_attempts()
print(user.login_attempts)
user.reset_login_attempts()
print(user.login_attempts)

上一篇:2022美亚个人赛复盘
下一篇:遨博机械臂——ROS通讯机制