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获取beautifulphoto随机某图片代码实例
Dec 18 Python
Python获取脚本所在目录的正确方法
Apr 15 Python
在Python的Flask框架中实现全文搜索功能
Apr 20 Python
python 文件操作api(文件操作函数)
Aug 28 Python
python进阶_浅谈面向对象进阶
Aug 17 Python
Python虚拟环境项目实例
Nov 20 Python
pycharm 主题theme设置调整仿sublime的方法
May 23 Python
python3中函数参数的四种简单用法
Jul 09 Python
浅谈Python中的可迭代对象、迭代器、For循环工作机制、生成器
Mar 11 Python
python爬虫 基于requests模块发起ajax的get请求实现解析
Aug 20 Python
python函数不定长参数使用方法解析
Dec 14 Python
浅谈Python线程的同步互斥与死锁
Mar 22 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
人族 Terran 魔法与科技
2020/03/14 星际争霸
教你如何把一篇文章按要求分段
2006/10/09 PHP
?算你??的 PHP 程式大小
2006/12/06 PHP
初级的用php写的采集程序
2007/03/16 PHP
PHP 验证登陆类分享
2015/03/13 PHP
php实现和c#一致的DES加密解密实例
2017/07/24 PHP
关于php支持的协议与封装协议总结(推荐)
2017/11/17 PHP
怎么让脚本或里面的函数在所有图片都载入完毕的时候执行
2006/10/17 Javascript
不同浏览器对回车提交表单的处理办法
2010/02/13 Javascript
js的压缩及jquery压缩探讨(提高页面加载性能/保护劳动成果)
2013/01/29 Javascript
创建、调用JavaScript对象的方法集锦
2014/12/24 Javascript
jQuery+slidereveal实现的面板滑动侧边展出效果
2015/03/14 Javascript
js实现鼠标移到链接文字弹出一个提示层的方法
2015/05/11 Javascript
全面解析JS字符串和正则表达式中的match、replace、exec等函数
2016/07/01 Javascript
jQuery使用Layer弹出层插件闪退问题
2016/12/22 Javascript
jQuery插件zTree实现更新根节点中第i个节点名称的方法示例
2017/03/08 Javascript
解决bootstrap下拉菜单点击立即隐藏bug的方法
2017/06/13 Javascript
原生JS实现的雪花飘落动画效果
2018/05/03 Javascript
微信小程序实现横向滚动导航栏效果
2019/12/12 Javascript
详解vue-router 动态路由下子页面多页共活的解决方案
2019/12/22 Javascript
Vue开发中常见的套路和技巧总结
2020/11/24 Vue.js
[13:38]2015国际邀请赛中国战队出征仪式
2015/05/29 DOTA
[36:29]2018DOTA2亚洲邀请赛 4.1 小组赛 A组加赛 LGD vs TNC
2018/04/02 DOTA
Python时间戳与时间字符串互相转换实例代码
2013/11/28 Python
Python3中_(下划线)和__(双下划线)的用途和区别
2019/04/26 Python
基于python3监控服务器状态进行邮件报警
2019/10/19 Python
简单介绍django提供的加密算法
2019/12/18 Python
Python+Django+MySQL实现基于Web版的增删改查的示例代码
2020/05/13 Python
python代码区分大小写吗
2020/06/17 Python
用python实现学生管理系统
2020/07/24 Python
Canvas与图片压缩的示例代码
2017/11/28 HTML / CSS
Perry Ellis官网:美国男士品味服装
2016/12/09 全球购物
美国领先的家庭健康检测试剂盒提供商:LetsGetChecked
2019/03/18 全球购物
2015年乡镇纪委工作总结
2015/05/26 职场文书
Spring this调用当前类方法无法拦截的示例代码
2022/03/20 Java/Android
MySQL数据库表约束讲解
2022/06/21 MySQL