zoukankan      html  css  js  c++  java
  • 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()
    

      


    ==============================================================================

    本博客已经废弃,不在维护。新博客地址:http://wenchao.ren


    我喜欢程序员,他们单纯、固执、容易体会到成就感;面对压力,能够挑灯夜战不眠不休;面对困难,能够迎难而上挑战自我。他
    们也会感到困惑与傍徨,但每个程序员的心中都有一个比尔盖茨或是乔布斯的梦想“用智慧开创属于自己的事业”。我想说的是,其
    实我是一个程序员

    ==============================================================================
  • 相关阅读:
    机器学习中数据缺失的处理及建模方法
    小样本学习(Few-Shot Learning)
    常见文本相似度计算法
    【FPGA ZYNQ Ultrascale+ MPSOC教程】33.BRAM实现PS与PL交互
    【紫光同创国产FPGA教程】【第十章】DDR3读写测试实验
    【紫光同创国产FPGA教程】【第九章】HDMI编程测试实验
    【紫光同创国产FPGA教程】【第八章】SD卡读写实验
    【紫光同创国产FPGA教程】【第七章】I2C接口EEPROM实验
    【紫光同创国产FPGA教程】【第六章】PDS下按键消抖实验
    【紫光同创国产FPGA教程】【第五章】串口收发实验
  • 原文地址:https://www.cnblogs.com/rollenholt/p/2128525.html
Copyright © 2011-2022 走看看