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中的sort方法使用详解
Jul 25 Python
Python os模块中的isfile()和isdir()函数均返回false问题解决方法
Feb 04 Python
python中的字典操作及字典函数
Jan 03 Python
python操作xlsx文件的包openpyxl实例
May 03 Python
对python中使用requests模块参数编码的不同处理方法
May 18 Python
PyQt5的PyQtGraph实践系列3之实时数据更新绘制图形
May 13 Python
ORM Django 终端打印 SQL 语句实现解析
Aug 09 Python
基于Python的图像数据增强Data Augmentation解析
Aug 13 Python
Django多进程滚动日志问题解决方案
Dec 17 Python
基于tensorflow for循环 while循环案例
Jun 30 Python
Pycharm新手使用教程(图文详解)
Sep 17 Python
Python正则表达式中flags参数的实例详解
Apr 01 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
农民和部队如何穿矿
2020/03/04 星际争霸
[EPIC] Larva vs Flash ZvT @ Crossing Field [2017-10-09]
2020/03/17 星际争霸
php获取远程图片体积大小的实例
2013/11/12 PHP
php比较两个绝对时间的大小
2014/01/31 PHP
php similar_text()函数的定义和用法
2016/05/12 PHP
PHP迭代与递归实现无限级分类
2017/08/28 PHP
JS画5角星方法介绍
2013/09/17 Javascript
jQuery中:checked选择器用法实例
2015/01/04 Javascript
浅谈node.js中async异步编程
2015/10/22 Javascript
javascript中Date format(js日期格式化)方法小结
2015/12/17 Javascript
AngularJS折叠菜单实现方法示例
2017/05/18 Javascript
Angular6 Filter实现页面搜索的示例代码
2018/12/02 Javascript
express+vue+mongodb+session 实现注册登录功能
2018/12/06 Javascript
详解使用JWT实现单点登录(完全跨域方案)
2019/08/02 Javascript
Vue指令之 v-cloak、v-text、v-html实例详解
2019/08/08 Javascript
微信小程序iOS下拉白屏晃动问题解决方案
2019/10/12 Javascript
vue实现微信浏览器左上角返回按钮拦截功能
2020/01/18 Javascript
js校验开始时间和结束时间
2020/05/26 Javascript
5个你不知道的JavaScript字符串处理库(小结)
2020/06/01 Javascript
详解Vue.js 响应接口
2020/07/04 Javascript
vue实现可移动的悬浮按钮
2021/03/04 Vue.js
Python 的 with 语句详解
2014/06/13 Python
python中的sort方法使用详解
2014/07/25 Python
python实现从ftp服务器下载文件的方法
2015/04/30 Python
Python类属性的延迟计算
2016/10/22 Python
浅谈Django中的数据库模型类-models.py(一对一的关系)
2018/05/30 Python
基于python指定包的安装路径方法
2018/10/27 Python
Notino罗马尼亚网站:购买香水和化妆品
2019/07/20 全球购物
如何用Java实现列出某个目录下的所有子目录
2015/07/20 面试题
年级组长自我鉴定
2014/02/22 职场文书
大学军训感言400字
2014/03/11 职场文书
学校庆元旦歌咏比赛主持词
2014/03/18 职场文书
团结演讲稿范文
2014/05/23 职场文书
团党委领导干部党的群众路线教育实践活动个人对照检查材料思想汇
2014/10/05 职场文书
中秋节晚会开场白
2015/05/29 职场文书
教你部署vue项目到docker
2022/04/05 Vue.js