9-1 餐馆/ 9-2 三家餐馆/ 9-3 用户
1. 动手试一试
2. 代码
class Restaurant(): def __init__(self, restaurant_name, cuisine_type): self.restaurant_name = restaurant_name self.cuisine_type = cuisine_type def describle_restaurant(self): print("This is " + self.restaurant_name.title(), "\nIt have " + str(self.cuisine_type) + " pieces of foods.") def open_restaurant(self): print("Now is opening...") restaurant = Restaurant('Luckin', 54) # 创建restaurant实例 restaurant.describle_restaurant() # 打印restaurant 属性 restaurant.open_restaurant() # 打印restaurant另一个属性 print("-----------------------------------------------------------") restaurant1 = Restaurant('Sweet center', 108 ) restaurant2 = Restaurant("KFC", 36) restaurant3 = Restaurant('Xiangtianxia', 18 ) restaurant1.describle_restaurant() restaurant2.describle_restaurant() restaurant3.describle_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 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?") user1 = User('Mike', 'Jhon', 28 ,'Anhui', 13141161718) # 实例化 user2 = User('Kevin', 'Durant', 30, 'Shanghai', 1213141516) user3 = User('Alex', 'Li', 24, 'Beijing', 1618191714) user1.describe_user() # 调用方法 user2.describe_user() user3.describe_user() user1.greet_user() # 调用方法 user2.greet_user() user3.greet_user()
3. 执行结果
This is Luckin It have 54 pieces of foods. Now is opening... ----------------------------------------------------------- This is Sweet Center It have 108 pieces of foods. This is Kfc It have 36 pieces of foods. This is Xiangtianxia It have 18 pieces of foods. @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Mike Jhon 28 Anhui 13141161718 Kevin Durant 30 Shanghai 1213141516 Alex Li 24 Beijing 1618191714 How beautiful name JhonJhon too young, too simple your homeland Anhui is a warm place, could you tell me your contact? How beautiful name DurantDurant too young, too simple your homeland Shanghai is a warm place, could you tell me your contact? How beautiful name LiLi too young, too simple your homeland Beijing is a warm place, could you tell me your contact? Process finished with exit code 0