python静态方法实例

作者:shichen2014 时间:2023-02-17 12:03:22 

本文实例讲述了python静态方法。分享给大家供大家参考。

具体实现方法如下:

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.

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()

希望本文所述对大家的Python程序设计有所帮助。

标签:python,静态,方法
0
投稿

猜你喜欢

  • python获取一组汉字拼音首字母的方法

    2023-04-08 16:43:02
  • Django项目如何配置Memcached和Redis缓存?选择哪个更有优势?

    2021-11-17 17:38:42
  • mysql、mssql及oracle分页查询方法详解

    2024-01-21 15:11:34
  • sql带分隔符的截取字符串示例

    2024-01-13 04:52:12
  • Python装饰器语法糖

    2022-07-10 15:11:34
  • python基于queue和threading实现多线程下载实例

    2023-02-04 09:58:22
  • asp显示数据库中表名、字段名、字段内容

    2008-06-17 18:09:00
  • 最新版MySQL 8.0.22下载安装超详细教程(Windows 64位)

    2024-01-18 15:11:02
  • Python中使用pprint函数进行格式化输出的教程

    2022-08-26 03:34:17
  • Python求一批字符串的最长公共前缀算法示例

    2021-03-10 13:40:37
  • python热力图实现简单方法

    2023-10-28 06:14:41
  • Linux添加Python path方法及修改环境变量的三种方法

    2021-03-26 12:22:45
  • vue proxytable代理根路径的同时增加其他代理方式

    2024-05-05 09:09:17
  • Python绘制简单散点图的方法

    2023-02-22 02:01:07
  • django框架模板语言使用方法详解

    2021-11-10 12:00:36
  • PHP获取客户端及服务器端IP的封装类

    2024-05-03 15:48:38
  • 详解利用python-highcharts库绘制交互式可视化图表

    2022-05-05 14:29:53
  • IE下,事件触发那点破烂事儿

    2009-04-27 12:31:00
  • Python接口测试环境搭建过程详解

    2021-08-31 16:12:52
  • Python操作Elasticsearch处理timeout超时

    2022-04-23 02:42:18
  • asp之家 网络编程 m.aspxhome.com