body
{
font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI",Tahoma,Helvetica,Sans-Serif,"Microsoft YaHei", Georgia,Helvetica,Arial,sans-serif,宋体, PMingLiU,serif;
font-size: 10.5pt;
line-height: 1.5;
}
html, body
{
}
h1 {
font-size:1.5em;
font-weight:bold;
}
h2 {
font-size:1.4em;
font-weight:bold;
}
h3 {
font-size:1.3em;
font-weight:bold;
}
h4 {
font-size:1.2em;
font-weight:bold;
}
h5 {
font-size:1.1em;
font-weight:bold;
}
h6 {
font-size:1.0em;
font-weight:bold;
}
img {
border:0;
max-width: 100%;
}
blockquote {
margin-top:0px;
margin-bottom:0px;
}
table {
border-collapse:collapse;
border:1px solid #bbbbbb;
}
td {
border-collapse:collapse;
border:1px solid #bbbbbb;
}
2011-03-24 11:58:06| 分类: python | 标签:python 类方法 静态方法 |举报|字号 订阅
转自http://stonelee.javaeye.com/blog/726449
使用方法:
Python代码 收藏代码
class A(object):
def foo(self,x):
#类实例方法
print "executing foo(%s,%s)"%(self,x)
@classmethod
def class_foo(cls,x):
#类方法
print "executing class_foo(%s,%s)"%(cls,x)
@staticmethod
def static_foo(x):
#静态方法
print "executing static_foo(%s)"%x
调用方法:
Python代码 收藏代码
a = A()
a.foo(1) //print : executing foo(<__main__.A object at 0xb77d67ec>,1)
a.class_foo(1) //executing class_foo(<class '__main__.A'>,1)
A.class_foo(1) //executing class_foo(<class '__main__.A'>,1)
a.static_foo(1) //executing static_foo(1)
A.static_foo(1) //executing static_foo(1)
区别:
类方法和静态方法都可以被类和类实例调用,类实例方法仅可以被类实例调用
类方法的隐含调用参数是类,而类实例方法的隐含调用参数是类的实例,静态方法没有隐含调用参数