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中使用动态变量名的方法
May 06 Python
python使用xlrd实现检索excel中某列含有指定字符串记录的方法
May 09 Python
Python 绘图和可视化详细介绍
Feb 11 Python
python3 pillow生成简单验证码图片的示例
Sep 19 Python
Python线程创建和终止实例代码
Jan 20 Python
对python Tkinter Text的用法详解
Oct 11 Python
PyQt打开保存对话框的方法和使用详解
Feb 27 Python
Python实现的远程文件自动打包并下载功能示例
Jul 12 Python
Python Numpy中数据的常用保存与读取方法
Apr 01 Python
基于python生成英文版词云图代码实例
May 16 Python
深入理解Pytorch微调torchvision模型
Nov 11 Python
方法汇总:Python 安装第三方库常用
Apr 26 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实现判断数组是一维、二维或几维的方法
2017/02/06 PHP
win10 apache配置虚拟主机后localhost无法使用的解决方法
2018/01/27 PHP
php curl获取到json对象并转成数组array的方法
2018/05/31 PHP
最佳的addEvent事件绑定是怎样诞生的
2011/10/24 Javascript
jQuery学习笔记(3)--用jquery(插件)实现多选项卡功能
2013/04/08 Javascript
js jq 单击和双击区分示例介绍
2013/11/05 Javascript
jQuery实现的原图对比窗帘效果
2014/06/15 Javascript
javascript实现设置、获取和删除Cookie的方法
2015/06/01 Javascript
JQuery插入DOM节点的方法
2015/06/11 Javascript
JavaScript开发Chrome浏览器扩展程序UI的教程
2016/05/16 Javascript
15个非常实用的JavaScript代码片段
2016/12/18 Javascript
Bootstrap popover用法详解
2016/12/22 Javascript
详解JS对象封装的常用方式
2016/12/30 Javascript
jQuery设计思想
2017/03/07 Javascript
使用Vue完成一个简单的todolist的方法
2017/12/01 Javascript
使用vue-aplayer插件时出现的问题的解决
2018/03/02 Javascript
又拍云 Node.js 实现文件上传、删除功能
2018/10/28 Javascript
Seajs源码详解分析
2019/04/02 Javascript
Vue 中如何正确引入第三方模块的方法步骤
2019/05/05 Javascript
layui点击弹框页面 表单请求的方法
2019/09/21 Javascript
谈谈IntersectionObserver懒加载的具体使用
2019/10/15 Javascript
jQuery 实现DOM元素拖拽交换位置的实例代码
2020/07/14 jQuery
关于uniApp editor微信滑动问题
2021/01/15 Javascript
Python如何脚本过滤文件中的注释
2020/05/27 Python
详解HTML5 data-* 自定义属性
2018/01/24 HTML / CSS
Footshop罗马尼亚:最好的运动鞋选择
2019/09/10 全球购物
正宗的澳大利亚Ugg靴子零售商:UGG Express
2020/04/19 全球购物
材料物理专业个人求职信
2013/12/15 职场文书
四好少年事迹材料
2014/01/12 职场文书
运动会广播稿60字
2014/01/15 职场文书
进步之星获奖感言
2014/02/22 职场文书
年度考核自我鉴定
2014/03/19 职场文书
计算机毕业大学生求职信
2014/06/26 职场文书
工程承包协议书
2014/10/20 职场文书
导游词之海南天涯海角
2019/12/05 职场文书
Python进行区间取值案例讲解
2021/08/02 Python