• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python静态方法实例

python静态方法实例

作者:shichen2014 字体:[增加 减小] 来源:互联网

shichen2014 通过本文主要向大家介绍了python 静态方法,python 静态变量,python 静态函数,python 静态类,python 静态成员等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了python静态方法。分享给大家供大家参考。

具体实现方法如下:

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()</div>

希望本文所述对大家的Python程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • python非递归全排列实现方法
  • Python中取整的几种方法小结
  • Python中取整的几种方法小结
  • python中常用的九种预处理方法分享
  • python自动翻译实现方法
  • Python计算已经过去多少个周末的方法
  • Python根据区号生成手机号码的方法
  • Python中实现三目运算的方法
  • python简单实现旋转图片的方法
  • python简单实现旋转图片的方法

相关文章

  • python3中dict(字典)的使用方法示例
  • 从零学python系列之新版本导入httplib模块报ImportError解决方案
  • python之文件的读写和文件目录以及文件夹的操作实现代码
  • 使用Python实现下载网易云音乐的高清MV
  • python采集博客中上传的QQ截图文件
  • Python批量按比例缩小图片脚本分享
  • python分析nignx访问日志脚本分享
  • python中的函数用法入门教程
  • 介绍Python的Django框架中的静态资源管理器django-pipeline
  • python编码最佳实践之总结

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • python实现域名系统(DNS)正向查询的方法
    • Python爬取网易云音乐热门评论
    • Mac OS X10.9安装的Python2.7升级Python3.3步骤详解
    • 使用python实现strcmp函数功能示例
    • Python中__init__和__new__的区别详解
    • 在Python中使用swapCase()方法转换大小写的教程
    • Python中使用不同编码读写txt文件详解
    • 用Python的线程来解决生产者消费问题的示例
    • 用python解析xml的几种方法
    • Python实现随机生成任意数量车牌号

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有