关于python的类方法、实例方法和静态方法区别
2024-10-10 19:15:45
python的类方法需要在方法前面加装饰器:@classmethod ,静态方法是在方法前面加装饰器:@staticmethod。
类方法、类属性是属于类自身,属于类自身的命名空间,和实例方法、实例属性不在一个命名空间下。
实例对象和类都可以直接调用类方法、实例方法和静态方法,区别在于:在类方法和静态方法内部不能调用实例属性。
class Base:
def __init__(self):
print "base begin..."
def prints(self):
print "print base..." class Base1:
def __init__(self):
print "base1 begin..."
def printss(self):
print "print base1..." class Base2(Base1,Base):
names = 'li'
def __init__(self,name):
self.name = name
def prints(self):
print "print objectmethod..."+self.name
@staticmethod
def printStatic():
print "print staticmethod..."
@classmethod
def printClass(cls):
print "print classmethod"+cls.names b = Base2('king')
b.prints()
b.printClass()
b.printStatic()
print b.names
Base2('jim').printStatic()
Base2('jim').printClass()
Base2('jim').prints()
最新文章
- 解决Asp.net Mvc中使用异步的时候HttpContext.Current为null的方法
- fastjson自动转化参数报错
- 介绍开源的.net通信框架NetworkComms框架 源码分析(七)ProtobufSerializer
- MYSQL:使用\G参数改变输出结果集的显示方式
- ETL工具与脚本实现之间的对比
- Bag Problem
- CI实践_Android持续集成
- js控制div是否显示
- C# rmi例子
- template_20_实现智能指针
- Android 虚拟机安装SD卡
- ServletContext中的转发
- jQuery Mobile基础
- Uva 1612 Guess
- 小白的Python之路 day5 logging模块
- Luogu3527 POI2011 Meteors 整体二分、树状数组、差分
- I2S接口介绍
- node.js中的http.response.end方法使用说明
- InstallShield卸载不彻底,残留大量dll文件
- 《GPU高性能编程CUDA实战》第九章 原子性