9-4 就餐人数/ 9-5 尝试登录次数
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | 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 ): print ( self .restaurant_name, self .cuisine_type) def open_restaurant( self ): print ( "Now is opening..." ) def read_restaurant( self ): print ( "The restaurant have " + str ( self .number_served) + ' people eating.' ) def set_number_served( self , member): # 添加方法,设置就餐人数 self .number_served = member def increment_number_served( self , members): # 添加方法,设置递增人数 self .number_served + = members restaurant = Restaurant( 'Kaoyu' , 10 ) # 创建实例 restaurant.read_restaurant() # 初始化设置为0 restaurant.number_served = 30 # 修改值 restaurant.read_restaurant() restaurant.set_number_served( 100 ) # 调用方法,传值 restaurant.read_restaurant() restaurant.increment_number_served( 200 ) 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 print ( "共登录用户量:" + str ( self .login_attempts)) def reset_login_attempts( self , number2): # 重置方法,重置为0 self .login_attempts = 0 print ( "共登录用户量:" + str ( self .login_attempts)) user = User( 'Michile' , 'Jadon' , 40 , 'Chicago' , 10089 ) # 创建实例 user.increment_login_attempts( 1 ) #调用递增方法 user.reset_login_attempts( 0 ) # 调用重置方法 user.increment_login_attempts( 1 ) #调用递增 user.increment_login_attempts( 1 ) #再次调用 user.increment_login_attempts( 1 ) #第三次调用,此时值应该为3(每次递增1) |
3. 执行结果
1 2 3 4 5 6 7 8 9 10 11 12 13 | D:\python编程:从入门到实践\venv\Scripts\python.exe D: / python编程:从入门到实践 / Restaturant02.py The restaurant have 0 people eating. The restaurant have 30 people eating. The restaurant have 100 people eating. The restaurant have 300 people eating. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 共登录用户量: 1 共登录用户量: 0 共登录用户量: 1 共登录用户量: 2 共登录用户量: 3 Process finished with exit code 0 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律