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 相关文章推荐
Python3基础之list列表实例解析
Aug 13 Python
Python中的匿名函数使用简介
Apr 27 Python
结合Python的SimpleHTTPServer源码来解析socket通信
Jun 27 Python
Windows下的Jupyter Notebook 安装与自定义启动(图文详解)
Feb 21 Python
使用django-crontab实现定时任务的示例
Feb 26 Python
python特性语法之遍历、公共方法、引用
Aug 08 Python
Python3 Post登录并且保存cookie登录其他页面的方法
Dec 28 Python
Python3 Tkinter选择路径功能的实现方法
Jun 14 Python
python之当你发现QTimer不能用时的解决方法
Jun 21 Python
pytorch中的embedding词向量的使用方法
Aug 18 Python
Python在OpenCV里实现极坐标变换功能
Sep 02 Python
python绘制BA无标度网络示例代码
Nov 21 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用ini_get获取php.ini里变量值的方法
2015/03/04 PHP
PHP7 整型处理机制修改
2021/03/09 PHP
js压缩利器
2007/02/20 Javascript
js获取url参数的使用扩展实例
2007/12/29 Javascript
[原创]IE view-source 无法查看看源码 JavaScript看网页源码
2009/07/19 Javascript
JavaScript 事件记录使用说明
2009/10/20 Javascript
input:checkbox多选框实现单选效果跟radio一样
2014/06/16 Javascript
jQuery中slideUp()方法用法分析
2014/12/24 Javascript
JavaScript 表单处理实现代码
2015/04/13 Javascript
javascript实现PC网页里的拖拽效果
2016/03/14 Javascript
关于网页中的无缝滚动的js代码
2016/06/09 Javascript
最丑的时钟效果!js canvas时钟制作方法
2016/08/15 Javascript
JS实现最简单的冒泡排序算法
2017/02/15 Javascript
创建简单的node服务器实例(分享)
2017/06/23 Javascript
js中bool值的转换及“&&”、“||”、 “!!”详解
2017/12/21 Javascript
详解javascript 正则表达式之分组与前瞻匹配
2018/05/30 Javascript
原生JavaScript创建不可变对象的方法简单示例
2020/05/07 Javascript
vue-cli3项目打包后自动化部署到服务器的方法
2020/09/16 Javascript
js重写alert事件(避免alert弹框标题出现网址)
2020/12/04 Javascript
vuex的使用步骤
2021/01/06 Vue.js
Python复制目录结构脚本代码分享
2015/03/06 Python
TensorFlow的权值更新方法
2018/06/14 Python
详解django自定义中间件处理
2018/11/21 Python
Pandas透视表(pivot_table)详解
2019/07/22 Python
Django发送邮件功能实例详解
2019/09/02 Python
Python selenium 加载并保存QQ群成员,去除其群主、管理员信息的示例代码
2020/05/28 Python
python中zip()函数遍历多个列表方法
2021/02/18 Python
css背景图片的背景裁切、背景透明度、背景变换等效果运用
2012/12/24 HTML / CSS
加拿大领先家居家具网上购物:Aosom.ca
2020/05/27 全球购物
初中校园广播稿
2014/02/02 职场文书
网上卖盒饭创业计划书范文
2014/02/07 职场文书
情人节活动策划方案
2014/02/27 职场文书
个人合作协议范本
2015/08/06 职场文书
关于开学的感想
2015/08/10 职场文书
浏览器常用基本操作之python3+selenium4自动化测试(基础篇3)
2021/05/21 Python
详细介绍Next.js脚手架完整搭建封装
2022/04/26 Javascript