0005python中的静态方法和类方法
2017-02-07 16:49
309 查看
#!/usr/bin/env python
# coding=utf-8
__metaclass__ = type
class StaticMethod:
@staticmethod
def foo():
print "This is static method foo()."
class ClassMethod:
@classmethod
def bar(cls):
print "This is class method bar()."
print "bar() is part of class:", cls.__name__
if __name__ == "__main__":
static_foo = StaticMethod() #实例化
static_foo.foo() #实例调用静态方法
StaticMethod.foo() #通过类来调用静态方法
print "********"
class_bar = ClassMethod()
class_bar.bar()
ClassMethod.bar()
C:\Users\Administrator>python d:\aa.py
This is static method foo().
This is static method foo().
********
This is class method bar().
bar() is part of class: ClassMethod
This is class method bar().
bar() is part of class: ClassMethod
# coding=utf-8
__metaclass__ = type
class StaticMethod:
@staticmethod
def foo():
print "This is static method foo()."
class ClassMethod:
@classmethod
def bar(cls):
print "This is class method bar()."
print "bar() is part of class:", cls.__name__
if __name__ == "__main__":
static_foo = StaticMethod() #实例化
static_foo.foo() #实例调用静态方法
StaticMethod.foo() #通过类来调用静态方法
print "********"
class_bar = ClassMethod()
class_bar.bar()
ClassMethod.bar()
C:\Users\Administrator>python d:\aa.py
This is static method foo().
This is static method foo().
********
This is class method bar().
bar() is part of class: ClassMethod
This is class method bar().
bar() is part of class: ClassMethod
相关文章推荐
- Python中静态方法的实现
- python基础3--面向对象--类变量、实例变量、类方法、静态方法、实例方法
- Python 静态方法和静态类
- Python中的静态方法-pybatis的准备阶段(五)
- [Python]静态方法和类方法
- Python中静态方法的实现
- Python的静态方法和类成员方法
- python里关于类方法和静态方法的区别
- python类方法和静态方法
- python中的静态成员方法 和 类成员方法
- [Dynamic Language] Python 静态方法、类方法、属性
- [总结]python 静态方法与类方法
- python类方法和静态方法
- 在python种实现类的静态方法
- PYTHON中的静态方法和类方法
- python的静态方法
- python的类方法和类的静态方法
- python 静态方法和类方法
- Python 静态方法与类方法