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 相关文章推荐
一个基于flask的web应用诞生 记录用户账户登录状态(6)
Apr 11 Python
Python数据结构之栈、队列的实现代码分享
Dec 04 Python
Python图像处理之简单画板实现方法示例
Aug 30 Python
Python面向对象之类的内置attr属性示例
Dec 14 Python
对python多线程中互斥锁Threading.Lock的简单应用详解
Jan 11 Python
检测python爬虫时是否代理ip伪装成功的方法
Jul 12 Python
python sorted方法和列表使用解析
Nov 18 Python
Pytorch模型转onnx模型实例
Jan 15 Python
使用遗传算法求二元函数的最小值
Feb 11 Python
python圣诞树编写实例详解
Feb 13 Python
用python实现一个简单计算器(完整DEMO)
Oct 14 Python
Python词云的正确实现方法实例
May 08 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实现生成随机水印图片
2020/12/09 PHP
自动生成文章摘要的代码[JavaScript 版本]
2007/03/20 Javascript
AJAX异步从优酷专辑中采集所有视频及信息(JavaScript代码)
2010/11/20 Javascript
jQuery实现用方向键控制层的上下左右移动
2013/01/13 Javascript
js substring从右边获取指定长度字符串(示例代码)
2013/12/23 Javascript
js取float型小数点后两位数的方法
2014/01/18 Javascript
jQuery对指定元素中指定字符串进行替换的方法
2015/03/17 Javascript
简介alert()与console.log()的不同
2015/08/26 Javascript
详解JavaScript的Date对象(制作简易钟表)
2020/04/07 Javascript
浅谈jQuery animate easing的具体使用方法(推荐)
2016/06/17 Javascript
JS获取checkbox的个数简单实例
2016/08/19 Javascript
jQuery+Ajax请求本地数据加载商品列表页并跳转详情页的实现方法
2017/07/12 jQuery
微信小程序保持session会话的方法
2020/03/20 Javascript
在vue中通过render函数给子组件设置ref操作
2020/11/17 Vue.js
如何封装Vue Element的table表格组件
2021/02/06 Vue.js
Python中使用logging模块代替print(logging简明指南)
2014/07/09 Python
python分析nignx访问日志脚本分享
2015/02/26 Python
Python中关于Sequence切片的下标问题详解
2017/06/15 Python
TensorFlow变量管理详解
2018/03/10 Python
Python实现发送与接收邮件的方法详解
2018/03/28 Python
Python实现的爬虫刷回复功能示例
2018/06/07 Python
Python matplotlib通过plt.scatter画空心圆标记出特定的点方法
2018/12/13 Python
ubuntu 18.04搭建python环境(pycharm+anaconda)
2019/06/14 Python
MNIST数据集转化为二维图片的实现示例
2020/01/10 Python
Python各种扩展名区别点整理
2020/02/27 Python
pandas 像SQL一样使用WHERE IN查询条件说明
2020/06/05 Python
Python利用myqr库创建自己的二维码
2020/11/24 Python
鼠标滚轮事件和Mac触控板双指事件
2019/12/23 HTML / CSS
法国在线宠物店:zooplus.fr
2018/02/23 全球购物
法国购买二手电子产品网站:Asgoodasnew
2020/03/27 全球购物
医院办公室主任职责
2013/12/29 职场文书
个人收入证明范本
2014/01/12 职场文书
新闻编辑专业毕业自荐书范文
2014/02/05 职场文书
党员承诺书内容
2014/03/26 职场文书
乌镇导游词
2015/02/02 职场文书
教师廉政准则心得体会
2016/01/20 职场文书