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 相关文章推荐
MySQL最常见的操作语句小结
May 07 Python
Python中常见的数据类型小结
Aug 29 Python
Python二叉搜索树与双向链表转换实现方法
Apr 29 Python
Python入门之三角函数全解【收藏】
Nov 08 Python
Python实现统计英文文章词频的方法分析
Jan 28 Python
元组列表字典(莫烦python基础)
Apr 03 Python
Python3中列表list合并的四种方法
Apr 19 Python
python 一篇文章搞懂装饰器所有用法(建议收藏)
Aug 23 Python
解决Python计算矩阵乘向量,矩阵乘实数的一些小错误
Aug 26 Python
Python填充任意颜色,不同算法时间差异分析说明
May 16 Python
python 密码学示例——凯撒密码的实现
Sep 21 Python
Django-Scrapy生成后端json接口的方法示例
Oct 06 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&&mysql)四
2006/10/09 PHP
PHP 应用程序的安全 -- 不能违反的四条安全规则
2006/11/26 PHP
php读取纯真ip数据库使用示例
2014/01/26 PHP
PHP文件操作实例总结【文件上传、下载、分页】
2018/12/08 PHP
Yii框架学习笔记之session与cookie简单操作示例
2019/04/30 PHP
Laravel使用模型实现like模糊查询的例子
2019/10/24 PHP
JqGrid web打印实现代码
2011/05/31 Javascript
javascript中onmouse事件在div中失效问题的解决方法
2012/01/09 Javascript
jQuery 处理页面的事件详解
2015/01/20 Javascript
js判断手机端(Android手机还是iPhone手机)
2015/07/22 Javascript
jQuery EasyUI Tab 选项卡问题小结
2016/08/16 Javascript
微信小程序 http请求详细介绍
2016/10/09 Javascript
快速掌握jQuery插件WebUploader文件上传
2016/11/07 Javascript
jQuery动态增减行的实例代码解析(推荐)
2016/12/05 Javascript
走进javascript——不起眼的基础,值和分号
2017/02/24 Javascript
nodejs 终端打印进度条实例代码
2017/04/22 NodeJs
js动态设置select下拉菜单的默认选中项实例
2018/08/21 Javascript
[07:48]DOTA2上海特级锦标赛主赛事首日RECAP
2016/03/04 DOTA
浅析Python中的for 循环
2016/06/09 Python
深入理解python中的闭包和装饰器
2016/06/12 Python
python中函数总结之装饰器闭包详解
2016/06/12 Python
python中set常用操作汇总
2016/06/30 Python
python实现K最近邻算法
2018/01/29 Python
解决python爬虫中有中文的url问题
2018/05/11 Python
使用Python向DataFrame中指定位置添加一列或多列的方法
2019/01/29 Python
python做接口测试的必要性
2019/11/20 Python
python3连接kafka模块pykafka生产者简单封装代码
2019/12/23 Python
用python3读取python2的pickle数据方式
2019/12/25 Python
Pyecharts 动态地图 geo()和map()的安装与用法详解
2020/03/25 Python
规范化管理年活动总结
2014/08/29 职场文书
迎国庆演讲稿
2014/09/15 职场文书
民间借贷纠纷案件代理词
2015/05/26 职场文书
请客吃饭开场白
2015/06/01 职场文书
小学课改工作总结
2015/08/13 职场文书
2016年中学法制宣传日活动总结
2016/04/01 职场文书
2019年圣诞节祝福语集锦
2019/12/25 职场文书