Python类成员方法与静态方法
2016-06-13 22:47
218 查看
Python中类属性有类属性和实例属性之分,类的方法也有不同的种类
实例方法
类方法
静态方法
例子:
class DemoMthd():
@staticmethod#静态方法
def static_mthd():
print("调用了静态方法")
@classmethod#类方法
def class_mthd(cls):
print("调用了类方法")
DemoMthd.static_mthd()
DemoMthd.class_mthd()
dm=DemoMthd()
dm.static_mthd()
dm.class_mthd()
对于这样的情况,对于类方法和静态方法,可以直接用类来调用也可以用实例来调用
但是不能使用实例属性,因为调用时类可能还没有实例化
实例方法
类方法
静态方法
例子:
class DemoMthd():
@staticmethod#静态方法
def static_mthd():
print("调用了静态方法")
@classmethod#类方法
def class_mthd(cls):
print("调用了类方法")
DemoMthd.static_mthd()
DemoMthd.class_mthd()
dm=DemoMthd()
dm.static_mthd()
dm.class_mthd()
对于这样的情况,对于类方法和静态方法,可以直接用类来调用也可以用实例来调用
但是不能使用实例属性,因为调用时类可能还没有实例化
相关文章推荐
- python3登录极路由并读取宽带帐号帐号密码.py
- python的datetime库笔记
- python函数学习笔记
- Python Socket
- Python类的属性
- Python包安装报错解决方法
- python实现线程池
- Python中的__name__和__main__含义详解
- python安装numpy函数库
- Python中的模块
- python numpy的学习
- python递归目录子目录列出目录和文件
- python 写爬虫
- Python main的命令行参数:sys.argv
- Python main的命令行参数:sys.argv
- Python main的命令行参数:sys.argv
- Python main的命令行参数:sys.argv
- Python main的命令行参数:sys.argv
- Python main的命令行参数:sys.argv
- Python main的命令行参数:sys.argv