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实现简单拆分PDF文件的方法
Jul 30 Python
Python随机数用法实例详解【基于random模块】
Apr 18 Python
基于并发服务器几种实现方法(总结)
Dec 29 Python
基于循环神经网络(RNN)的古诗生成器
Mar 26 Python
Python socket实现简单聊天室
Apr 01 Python
Django数据库类库MySQLdb使用详解
Apr 28 Python
如何基于Python Matplotlib实现网格动画
Jul 20 Python
Django Admin后台模型列表页面如何添加自定义操作按钮
Nov 11 Python
python 遍历磁盘目录的三种方法
Apr 02 Python
python数据库批量插入数据的实现(executemany的使用)
Apr 30 Python
python实现自动清理文件夹旧文件
May 10 Python
python 中yaml文件用法大全
Jul 04 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正则匹配汉字的方法介绍
2013/04/25 PHP
Zend Framework教程之Application用法实例详解
2016/03/14 PHP
PHP引用的调用方法分析
2016/04/25 PHP
php中this关键字用法分析
2016/12/07 PHP
一种JavaScript的设计模式
2006/11/22 Javascript
FusionCharts图表显示双Y轴双(多)曲线
2012/11/22 Javascript
如何使用JS获取IE上传文件路径(IE7,8)
2013/07/08 Javascript
JQuery+Ajax无刷新分页的实例代码
2014/02/08 Javascript
JavaScript DOM操作表格及样式
2015/04/13 Javascript
javascript实现图片延迟加载方法汇总(三种方法)
2015/08/27 Javascript
JavaScript基本的输出和嵌入式写法教程
2015/10/20 Javascript
JS模拟按钮点击功能的方法
2015/12/22 Javascript
JS日期对象简单操作(获取当前年份、星期、时间)
2016/10/26 Javascript
jQuery实现在HTML文档加载完毕后自动执行某个事件的方法
2017/05/08 jQuery
vue-cli之router基本使用方法详解
2017/10/17 Javascript
利用vue.js实现被选中状态的改变方法
2018/02/08 Javascript
jQuery控制input只能输入数字和两位小数的方法
2019/05/16 jQuery
Vue的路由及路由钩子函数的实现
2019/07/02 Javascript
[01:00:30]TFT vs VGJ.T Supermajor 败者组 BO3 第一场 6.5
2018/06/06 DOTA
Python中用altzone()方法处理时区的教程
2015/05/22 Python
python保存字符串到文件的方法
2015/07/01 Python
Python面向对象编程中关于类和方法的学习笔记
2016/06/30 Python
django基础学习之send_mail功能
2019/08/07 Python
python装饰器练习题及答案
2019/11/01 Python
python GUI库图形界面开发之PyQt5滑块条控件QSlider详细使用方法与实例
2020/02/28 Python
如何把python项目部署到linux服务器
2020/08/26 Python
解决Python 函数声明先后顺序出现的问题
2020/09/02 Python
Python实现简单的猜单词小游戏
2020/10/28 Python
Interflora澳大利亚:同日鲜花速递
2019/06/25 全球购物
Ellesse英国官网:意大利高级运动品牌
2019/07/23 全球购物
小学教师学期末自我评价
2013/09/25 职场文书
公司营业员的自我评价
2014/03/04 职场文书
3分钟英语演讲稿
2014/04/29 职场文书
2014年最新版离婚协议书范本
2014/11/25 职场文书
借条如何写
2015/05/26 职场文书
TypeScript中条件类型精读与实践记录
2021/10/05 Javascript