一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - python靜態方法實例

python靜態方法實例

2020-05-19 09:11腳本之家 Python

這篇文章主要介紹了python靜態方法,實例分析了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程序設計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 青青操在线播放 | 国产一区二区三区欧美 | 国产日韩免费视频 | 色久天| 日韩人成| 青青久久精品国产 | 亚洲精品国产成人7777 | 美女被视频网站 | 久草在在线免视频在线观看 | 国产一区二区免费在线 | 男人的天堂comwww | 国产成人精品高清不卡在线 | 国产亚洲精品第一综合另类 | 热99re久久精品精品免费 | 亚洲第一在线播放 | 精品久久久久免费极品大片 | 小妇人电影免费完整观看2021 | 天美影视传媒mv直接看 | 亚洲欧美一区二区三区不卡 | 双性小说肉 | 国产欧美一区二区三区久久 | 四虎免费入口 | 色综合天天娱乐综合网 | 日韩精品福利视频一区二区三区 | 色综合亚洲天天综合网站 | 久久亚洲电影www电影网 | 日韩视频免费一区二区三区 | 国产麻豆精品免费视频 | 国产东北三老头伦一肥婆 | 紧身牛仔裤美女被啪啪久久网 | 69欧美性猛交 | 日本zzzzwww大片免费 | 久久亚洲一级α片 | 亚洲精品国偷拍自产在线观看蜜臀 | 99色亚洲| 四虎884aa永久播放地址http | 天堂网在线.www天堂在线视频 | 国产二区视频 | 天美蜜桃精东乌鸦传媒 | 国产一区二 | 丝袜足控免费网站xx动漫漫画 |