python的静态方法
2011-08-05 13:20 Rollen Holt 阅读(769) 评论(0) 编辑 收藏 举报1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | staticmethod Found at: __builtin__ staticmethod (function) - > method Convert a function to be a static method. A static method does not receive an implicit first argument. To declare a static method, use this idiom: class C: def f(arg1, arg2, ...): ... f = staticmethod (f) It can be called either on the class (e.g. C.f()) or on an instance (e.g. C().f()). The instance is ignored except for its class . Static methods in Python are similar to those found in Java or C + + . For a more advanced concept, see the classmethod builtin. |
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 Employee: """Employee class with static method isCrowded""" numberOfEmployees = 0 # number of Employees created maxEmployees = 10 # maximum number of comfortable employees def isCrowded(): """Static method returns true if the employees are crowded""" return Employee.numberOfEmployees > Employee.maxEmployees # create static method isCrowded = staticmethod (isCrowded) def __init__( self , firstName, lastName): """Employee constructor, takes first name and last name""" self .first = firstName self .last = lastName Employee.numberOfEmployees + = 1 def __del__( self ): """Employee destructor""" Employee.numberOfEmployees - = 1 def __str__( self ): """String representation of Employee""" return "%s %s" % ( self .first, self .last) # main program def main(): answers = [ "No" , "Yes" ] # responses to isCrowded employeeList = [] # list of objects of class Employee # call static method using class print "Employees are crowded?" , print answers[ Employee.isCrowded() ] print "\nCreating 11 objects of class Employee..." # create 11 objects of class Employee for i in range ( 11 ): employeeList.append(Employee( "John" , "Doe" + str (i))) # call static method using object print "Employees are crowded?" , print answers[ employeeList[ i ].isCrowded() ] print "\nRemoving one employee..." del employeeList[ 0 ] print "Employees are crowded?" , answers[ Employee.isCrowded() ] if __name__ = = "__main__" : main() |
==============================================================================
本博客已经废弃,不在维护。新博客地址:http://wenchao.ren
我喜欢程序员,他们单纯、固执、容易体会到成就感;面对压力,能够挑灯夜战不眠不休;面对困难,能够迎难而上挑战自我。他
们也会感到困惑与傍徨,但每个程序员的心中都有一个比尔盖茨或是乔布斯的梦想“用智慧开创属于自己的事业”。我想说的是,其
实我是一个程序员
==============================================================================
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 数据库服务器 SQL Server 版本升级公告
· C#/.NET/.NET Core技术前沿周刊 | 第 23 期(2025年1.20-1.26)
· 程序员常用高效实用工具推荐,办公效率提升利器!