1. 动手试一试

2. 代码
class Restaurant():
def __init__(self, restaurant_name, cuisine_type):
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
self.number_served = 0 # 添加属性,设置默认值为0
def describle_restaurant(self):
long_name = self.restaurant_name + " have " + str(self.cuisine_type) + " kinds of food."
return long_name.title()
def open_restaurant(self):
print("Now is opening...")
def read_restaurant(self):
print("The restaurant have " + str(self.number_served) + ‘ people eating.‘)
def update_restaurant(self, numbers):
self.number_served = numbers
if numbers >= self.number_served:
self.number_served = numbers
else:
print("The reserve is full.")
def set_number_served(self, member): # 添加方法,设置就餐人数
self.number_served = member
def increment_number_served(self, members): # 添加方法,设置递增人数
self.number_served += members
restaurant = Restaurant(‘Kaoyu‘,10) # 创建实例
print(restaurant.describle_restaurant()) # 打印餐厅信息
restaurant.read_restaurant()
restaurant.update_restaurant(20)
restaurant.read_restaurant()
restaurant.set_number_served(30) # 调用方法,传值打印
restaurant.read_restaurant()
restaurant.increment_number_served(100)
restaurant.read_restaurant()
print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
class User(): # 创建User类
def __init__(self, first_name, last_name, age, address, phone): # 属性
self.first_name = first_name
self.last_name = last_name
self.age = age
self.address = address
self.phone = phone
self.login_attempts = 0 # 添加属性
def describe_user(self): # 方法
print(self.first_name,
self.last_name,
self.age,
self.address,
self.phone)
def greet_user(self): # 方法
print("How beautiful name " + self.last_name + self.last_name,
"\n too young, too simple", "your homeland " + self.address
+ " is a warm place, ", "could you tell me your contact?")
def increment_login_attempts(self, number1): # 递增方法
self.login_attempts += number1 # 值加1
def reset_login_attempts(self, number2): # 重置方法,重置为0
self.login_attempts = 0
user = User(‘Michile‘, ‘Jadon‘, 40, ‘Chicago‘, 10089) # 创建实例
user.increment_login_attempts(1) #调用递增方法
print(user.login_attempts) #打印值
user.reset_login_attempts(0) # 调用重置方法
print(user.login_attempts)
user.increment_login_attempts(1) #调用递增
user.increment_login_attempts(1) #再次调用
user.increment_login_attempts(1) #第三次调用,此时值应该为3(每次递增1)
print(user.login_attempts)
3. 运行结果
D:\python编程:从入门到实践\venv\Scripts\python.exe D:/python编程:从入门到实践/Restaturant02.py Kaoyu Have 10 Kinds Of Food. The restaurant have 0 people eating. The restaurant have 20 people eating. The restaurant have 30 people eating. The restaurant have 130 people eating. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 1 0 3 Process finished with exit code 0
原文:https://www.cnblogs.com/kevin-hou1991/p/14791204.html