python静态方法实例

yipeiwu_com5年前Python基础

本文实例讲述了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的线程来解决生产者消费问题的示例

我们将使用Python线程来解决Python中的生产者—消费者问题。这个问题完全不像他们在学校中说的那么难。 如果你对生产者—消费者问题有了解,看这篇博客会更有意义。 为什么要关心生产者...

详解如何为eclipse安装合适版本的python插件pydev

详解如何为eclipse安装合适版本的python插件pydev

pydev是一款优秀的Eclipse插件,大多数喜欢在eclipse开发软件的程序员(也许是java程序员)在开发python软件时希望继续使用eclipse,那么pydev是非常理想的...

Python将多个list合并为1个list的方法

Python将多个list合并为1个list的方法

1、可以使用"+"号完成操作 输出为: [1, 2, 3, 8, 'google', 'com'] 2、使用extend方法 输入相同 3、使用切片 输出相同 PS:len(l1)...

Django admin美化插件suit使用示例

Django admin美化插件suit使用示例

本文主要对Django美化插件做一个简单介绍,具体如下。 Django Suit 效果 使用前django页面 使用后django页面 安装 官方文档 http://django-s...

如何使用Python自动控制windows桌面

如何使用Python自动控制windows桌面

前言 在使用PC时与PC交互的主要途径是看屏幕显示、听声音,点击鼠标和敲键盘等等。在自动化办公的趋势下,繁琐的工作可以让程序自动完成。比如自动化测试、自动下单交易等。很多软件除了可以GU...