9-1 餐馆/ 9-2 三家餐馆/ 9-3 用户

1. 动手试一试

 

 2. 代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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. 执行结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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

  

posted @   JRS077  阅读(134)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示