国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 华蓥市| 辽中县| 津南区| 安陆市| 长垣县| 星座| 苏尼特左旗| 富阳市| 偏关县| 蚌埠市| 灌阳县| 遵化市| 禹州市| 瓮安县| 太湖县| 普陀区| 海盐县| 无为县| 洛阳市| 大竹县| 德安县| 久治县| 卓资县| 桓台县| 开远市| 德昌县| 兴化市| 隆尧县| 小金县| 黔西县| 西峡县| 西吉县| 富裕县| 武陟县| 元氏县| 全南县| 彭阳县| 宾川县| 阿克苏市| 酉阳| 温宿县|