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中bisect模块用法实例
Sep 25 Python
举例讲解如何在Python编程中进行迭代和遍历
Jan 19 Python
浅谈Python的文件类型
May 30 Python
python 循环while和for in简单实例
Aug 16 Python
使用python读取txt文件的内容,并删除重复的行数方法
Apr 18 Python
python的几种矩阵相乘的公式详解
Jul 10 Python
windows下python虚拟环境virtualenv安装和使用详解
Jul 16 Python
Python完成哈夫曼树编码过程及原理详解
Jul 29 Python
wxPython实现分隔窗口
Nov 19 Python
在tensorflow中设置使用某一块GPU、多GPU、CPU的操作
Feb 07 Python
python实现小程序推送页面收录脚本
Apr 20 Python
Python通过队列来实现进程间通信的示例
Oct 14 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
《OVERLORD》第四季,终于等到你!
2020/03/02 日漫
php+ajax做仿百度搜索下拉自动提示框(有实例)
2012/08/21 PHP
php preg_replace替换实例讲解
2013/11/04 PHP
刷新PHP缓冲区为你的站点加速
2015/10/10 PHP
php 实现简单的登录功能示例【基于thinkPHP框架】
2019/12/02 PHP
Javascript面向对象编程(二) 构造函数的继承
2011/08/28 Javascript
理解JavaScript的prototype属性
2012/02/11 Javascript
Javascript 面向对象(三)接口代码
2012/05/23 Javascript
关于jQuery对象数据缓存Cache原理以及jQuery.data详解
2013/04/07 Javascript
Javascript自定义函数判断网站访问类型是PC还是移动终端
2014/01/10 Javascript
jquery为页面增加快捷键示例
2014/01/31 Javascript
Jquery判断radio、selelct、checkbox是否选中及获取选中值方法总结
2015/04/15 Javascript
javascript无刷新评论实现方法
2015/05/13 Javascript
jQuery表单事件实例代码分享
2016/08/18 Javascript
jQuery表格(Table)基本操作实例分析
2017/03/10 Javascript
angularjs ui-router中路由的二级嵌套
2017/03/10 Javascript
ES6新特性之字符串的扩展实例分析
2017/04/01 Javascript
基于vue 添加axios组件,解决post传参数为null的问题
2018/03/05 Javascript
Layui点击图片弹框预览的实现方法
2019/09/16 Javascript
JS使用正则表达式判断输入框失去焦点事件
2019/10/16 Javascript
JS中准确判断变量类型的方法
2020/06/01 Javascript
JavaScript原生数组函数实例汇总
2020/10/14 Javascript
Python的迭代器和生成器
2015/07/29 Python
使用python实现抓取腾讯视频所有电影的爬虫
2019/04/15 Python
Python字典的概念及常见应用实例详解
2019/10/30 Python
python爬虫模块URL管理器模块用法解析
2020/02/03 Python
Python基于smtplib协议实现发送邮件
2020/06/03 Python
python中delattr删除对象方法的代码分析
2020/12/15 Python
解决tensorflow模型压缩的问题_踩坑无数,总算搞定
2021/03/02 Python
几道数据库的概念性面试题
2014/05/30 面试题
大学生就业自我鉴定
2013/10/26 职场文书
模具数控专业自荐信
2014/01/27 职场文书
项目采购员岗位职责
2014/04/15 职场文书
2014年旅游局法制宣传日活动总结
2014/11/01 职场文书
2015年英语教研组工作总结
2015/05/23 职场文书
解决vue自定义组件@click点击失效问题
2022/04/30 Vue.js