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

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

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程序設計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 免费在线观看日韩 | 日韩国产成人精品视频人 | 国产一区风间由美在线观看 | 日韩欧美中文在线 | 动漫美女被褥吸奶漫画漫画 | 日韩首页| 国产欧美一区二区三区精品 | 50度灰破解版v5.7.0 | 精品国产精品人妻久久无码五月天 | 美女张开腿让男人桶的 视频 | 极品91 | 日韩一级片在线播放 | 桥本有菜作品在线 | 日产一区二区 | 成人看的羞羞视频免费观看 | 久久久精品成人免费看 | 性欧美video| 羞羞色男人的天堂伊人久久 | 韩国三级做爰 | 国产成人愉拍免费视频 | 三上悠亚久久国产 | 亚洲经典 | 公妇仑乱在线观看 | 亚洲黄色高清 | 91你懂的 | 小仙夜晚慰自催眠mp3护士篇 | 久久黄色精品视频 | 日本老妇人乱视频 | yy8090韩国日本三理论免费 | 疯狂刺激的3p国产在线 | 国产精品视频久久久久 | 日本免费不卡在线一区二区三区 | 9热在线精品视频观看 | 青青热久久综合网伊人 | 手机看片黄色 | 国产成人亚洲综合a∨婷婷 国产成人亚洲精品乱码在线观看 | 日本免费的一级绿象 | 俺去啦最新 | 91免费在线播放 | 国产色网 | 久久精品中文闷骚内射 |