python静态方法实例


Posted in Python onJanuary 14, 2015

本文实例讲述了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程序设计有所帮助。

Python 相关文章推荐
通过代码实例展示Python中列表生成式的用法
Mar 31 Python
Python3读取zip文件信息的方法
May 22 Python
在Python中操作时间之mktime()方法的使用教程
May 22 Python
python运行时间的几种方法
Jun 17 Python
python利用urllib实现爬取京东网站商品图片的爬虫实例
Aug 24 Python
基于Python实现的ID3决策树功能示例
Jan 02 Python
python编程使用协程并发的优缺点
Sep 20 Python
python2.7实现邮件发送功能
Dec 12 Python
Python3连接Mysql8.0遇到的问题及处理步骤
Feb 17 Python
python matplotlib:plt.scatter() 大小和颜色参数详解
Apr 14 Python
使用OpenCV获取图像某点的颜色值,并设置某点的颜色
Jun 02 Python
Python生成九宫格图片的示例代码
Apr 14 Python
python继承和抽象类的实现方法
Jan 14 #Python
python列表操作实例
Jan 14 #Python
python操作gmail实例
Jan 14 #Python
Python中的装饰器用法详解
Jan 14 #Python
python登陆asp网站页面的实现代码
Jan 14 #Python
Python的面向对象思想分析
Jan 14 #Python
为python设置socket代理的方法
Jan 14 #Python
You might like
PHP使用strtotime计算两个给定日期之间天数的方法
2015/03/18 PHP
Array对象方法参考
2006/10/03 Javascript
javascript 兼容FF的onmouseenter和onmouseleave的代码
2008/07/19 Javascript
从JavaScript 到 JQuery (1)学习小结
2009/02/12 Javascript
javascript中的onkeyup和onkeydown区别介绍
2013/04/28 Javascript
jquery获取一组checkbox的值(实例代码)
2013/11/04 Javascript
JavaScript 字符串数字左补位,右补位,取固定长度,截位扩展函数代码
2017/03/25 Javascript
详解vue mint-ui源码解析之loadmore组件
2017/10/11 Javascript
React Form组件的实现封装杂谈
2018/05/07 Javascript
浅谈使用mpvue开发小程序需要注意和了解的知识点
2018/05/23 Javascript
vue-cli3.0+element-ui上传组件el-upload的使用
2018/12/03 Javascript
详解vue中使用protobuf踩坑记
2019/05/07 Javascript
[40:55]DOTA2上海特级锦标赛主赛事日 - 2 败者组第二轮#4Newbee VS Fnatic
2016/03/03 DOTA
[02:23]完美世界全国高校联赛街访DOTA2第一期
2019/11/28 DOTA
点球小游戏python脚本
2018/05/22 Python
Python文件常见操作实例分析【读写、遍历】
2018/12/10 Python
Django+JS 实现点击头像即可更改头像的方法示例
2018/12/26 Python
Numpy将二维数组添加到空数组的实现
2019/12/05 Python
Python动态导入模块和反射机制详解
2020/02/18 Python
Pytorch转onnx、torchscript方式
2020/05/25 Python
python pymysql链接数据库查询结果转为Dataframe实例
2020/06/05 Python
Myprotein丹麦官网:欧洲第一运动营养品牌
2019/04/15 全球购物
LN-CC英国:伦敦时尚生活的缩影
2019/09/01 全球购物
自动化专业毕业生自荐信
2013/11/01 职场文书
致1500米运动员广播稿
2014/02/07 职场文书
交通安全责任书范本
2014/07/24 职场文书
德育标兵事迹材料
2014/08/24 职场文书
餐厅感恩节活动策划方案
2014/10/11 职场文书
出国留学单位推荐信
2015/03/26 职场文书
2016年大学生暑期社会实践方案
2015/11/26 职场文书
教师廉政准则心得体会
2016/01/20 职场文书
幼儿园六一儿童节开幕词
2016/03/04 职场文书
纪念建国70周年演讲稿
2019/07/19 职场文书
2021-4-5课程——SQL Server查询【3】
2021/04/05 SQL Server
教你怎么用Python监控愉客行车程
2021/04/29 Python
提取视频中的音频 Python只需要三行代码!
2021/05/10 Python