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实现类之间的方法互相调用
Apr 29 Python
解决pycharm界面不能显示中文的问题
May 23 Python
Python实现正则表达式匹配任意的邮箱方法
Dec 20 Python
Python中字符串String的基本内置函数与过滤字符模块函数的基本用法
May 27 Python
机器学习实战之knn算法pandas
Jun 22 Python
Django文件存储 默认存储系统解析
Aug 02 Python
python3使用print打印带颜色的字符串代码实例
Aug 22 Python
Django admin 实现search_fields精确查询实例
Mar 30 Python
python实现Oracle查询分组的方法示例
Apr 30 Python
python golang中grpc 使用示例代码详解
Jun 03 Python
python 基于opencv实现图像增强
Dec 23 Python
Python机器学习应用之基于线性判别模型的分类篇详解
Jan 18 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
教你在PHPStorm中配置Xdebug
2015/07/27 PHP
PHP控制反转(IOC)和依赖注入(DI)
2017/03/13 PHP
PHP 二维关联数组根据其中一个字段排序(推荐)
2017/04/04 PHP
Laravel Eloquent分表方法并使用模型关联的实现
2019/11/25 PHP
javascript css styleFloat和cssFloat
2010/03/15 Javascript
从零开始学习jQuery (二) 万能的选择器
2010/10/01 Javascript
20个非常棒的 jQuery 幻灯片插件和教程分享
2011/08/23 Javascript
jQuery操作cookie方法实例教程
2014/11/25 Javascript
jquery中$each()方法的使用指南
2015/04/30 Javascript
基于Jquery插件实现跨域异步上传文件功能
2016/04/26 Javascript
使用nodejs中httpProxy代理时候出现404异常的解决方法
2016/08/15 NodeJs
js 定位到某个锚点的方法
2016/11/19 Javascript
Vue computed计算属性的使用方法
2017/07/14 Javascript
Node做中转服务器转发接口
2017/10/18 Javascript
vue 录制视频并压缩视频文件的方法
2018/07/27 Javascript
vue后台管理之动态加载路由的方法
2018/08/13 Javascript
微信小程序实现获取用户信息并存入数据库操作示例
2019/05/07 Javascript
vue+element项目中过滤输入框特殊字符小结
2019/08/07 Javascript
Nuxt默认模板、默认布局和自定义错误页面的实现
2020/05/11 Javascript
[30:37]【全国守擂赛】第三周擂主赛 Dark Knight vs. Leopard Gaming
2020/05/04 DOTA
举例讲解Python中的list列表数据结构用法
2016/03/12 Python
通过源码分析Python中的切片赋值
2017/05/08 Python
python 对象和json互相转换方法
2018/03/22 Python
Python numpy 提取矩阵的某一行或某一列的实例
2018/04/03 Python
python3中类的继承以及self和super的区别详解
2019/06/26 Python
推荐8款常用的Python GUI图形界面开发框架
2020/02/23 Python
Python替换NumPy数组中大于某个值的所有元素实例
2020/06/08 Python
MyHeritage美国:家族史研究和DNA测试的领先服务
2019/05/27 全球购物
Farah官方网站:男士服装及配件
2019/11/01 全球购物
儿科护士实习自我鉴定
2013/10/17 职场文书
体育老师的教学自我评价分享
2013/11/19 职场文书
重阳节标语大全
2014/10/07 职场文书
2015羊年春节慰问信
2015/02/14 职场文书
幼师个人总结范文
2015/02/28 职场文书
2015年教师党员自我评价材料
2015/03/04 职场文书