python从入门到实践动手试一试
时间:2021-05-20 00:46:54
收藏:0
阅读:14
1 class Restaurant(): 2 def __init__(self, restaurant_name, cuisine_type): 3 self.restaurant_name = restaurant_name 4 self.cuisine_type = cuisine_type 5 6 def describle_restaurant(self): 7 print("This is " + self.restaurant_name.title(), 8 "\nIt have " + str(self.cuisine_type) + " pieces of foods.") 9 10 def open_restaurant(self): 11 print("Now is opening...") 12 13 restaurant = Restaurant(‘Luckin‘, 54) # 创建restaurant实例 14 restaurant.describle_restaurant() # 打印restaurant 属性 15 restaurant.open_restaurant() # 打印restaurant另一个属性 16 print("-----------------------------------------------------------") 17 18 19 restaurant1 = Restaurant(‘Sweet center‘, 108 ) 20 restaurant2 = Restaurant("KFC", 36) 21 restaurant3 = Restaurant(‘Xiangtianxia‘, 18 ) 22 23 restaurant1.describle_restaurant() 24 restaurant2.describle_restaurant() 25 restaurant3.describle_restaurant() 26 print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@") 27 28 class User(): # 创建User类 29 def __init__(self, first_name, last_name, age, address, phone): # 属性 30 self.first_name = first_name 31 self.last_name = last_name 32 self.age = age 33 self.address = address 34 self.phone = phone 35 36 def describe_user(self): # 方法 37 print(self.first_name, 38 self.last_name, 39 self.age, 40 self.address, 41 self.phone) 42 43 def greet_user(self): # 方法 44 print("How beautiful name " + self.last_name + self.last_name, 45 "\n too young, too simple", "your homeland " + self.address 46 + " is a warm place, ", "could you tell me your contact?") 47 48 user1 = User(‘Mike‘, ‘Jhon‘, 28 ,‘Anhui‘, 13141161718) # 实例化 49 user2 = User(‘Kevin‘, ‘Durant‘, 30, ‘Shanghai‘, 1213141516) 50 user3 = User(‘Alex‘, ‘Li‘, 24, ‘Beijing‘, 1618191714) 51 52 user1.describe_user() # 调用方法 53 user2.describe_user() 54 user3.describe_user() 55 56 user1.greet_user() # 调用方法 57 user2.greet_user() 58 user3.greet_user()
原文:https://www.cnblogs.com/mayizhuangyuan/p/14787024.html
评论(0)