国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python靜態(tài)方法實(shí)例

2020-02-23 06:19:10
字體:
供稿:網(wǎng)友

本文實(shí)例講述了python靜態(tài)方法。分享給大家供大家參考。

具體實(shí)現(xiàn)方法如下:

代碼如下: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..."

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 佛教| 比如县| 枣庄市| 托克逊县| 错那县| 宿州市| 车险| 河津市| 扎囊县| 澄迈县| 崇文区| 桦南县| 富源县| 永德县| 扎赉特旗| 宁津县| 天柱县| 当雄县| 盘山县| 兴化市| 家居| 柞水县| 西畴县| 岱山县| 乌拉特后旗| 牡丹江市| 新巴尔虎左旗| 贞丰县| 普兰店市| 尤溪县| 名山县| 五华县| 合阳县| 淳化县| 兴义市| 巴彦淖尔市| 磐石市| 阳谷县| 新密市| 永靖县| 贵溪市|