9159金沙游艺场-奥门金沙游艺场-[欢迎您]

9159金沙游艺场为您呈现最新的面貌!,目前注册送体验金,欢迎您将成为影视娱乐行业新的风向标,是亚洲的优质娱乐先锋。

python静态方法实例9159金沙游艺场

日期:2020-01-04编辑作者:办公软件

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"""
9159金沙游艺场, 
      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程序设计有所帮助。

本文实例讲述了python静态方法。分享给大家供大家参考。 具体实现方法如下: 复制代码 代码如下: sta...

本文由9159金沙游艺场-奥门金沙游艺场-[欢迎您]发布于办公软件,转载请注明出处:python静态方法实例9159金沙游艺场

关键词:

CSS中父对象的内边距是否对子对象的外边距造成

CSS中父对象的内边距是否对子对象的外边距造成影响,css对子 CSS中父对象的内边距是否对子对象的外边距造成影响...

详细>>

CentOS7安装JAVA【9159金沙游艺场】

CentOS7安装JAVA 第一步:查看Linux自带的JDK是否已安装 安装好的CentOS会自带OpenJdk,用命令 java -version ,会有下面的信息:...

详细>>

linux调节源码

linux调治器源码解析,linux调整源码 本文为原创,转发请声明:   引言 在此以前的随笔已经将调解器的数据结构、起...

详细>>

Python连接mssql数据库编码问题解决方法9159金沙游

Python连接mssql数据库编码问题解决方法,pythonmssql python一直对中文支持的不好,最近老遇到编码问题,而且几乎没有通...

详细>>