Python中@staticmethod和@classmethod的区别和用法
xk-wang:
这个区别解释的不合理,你把代码改成下面的两个还是没区别。[code=python]
# -*- coding: utf-8 -*-
class Parent(object):
@staticmethod
def staticSayHello(anything):
if anything == Boy:
print("Boy classSayHello")
elif anything == Girl:
print("girl sayHello")
@classmethod
def classSayHello(anything): #这里是anything
if anything == Boy:
print("Boy classSayHello")
elif anything == Girl:
print("girl sayHello")
class Boy(Parent):
pass
class Girl(Parent):
pass
if __name__ == '__main__':
Boy.classSayHello()
Girl.classSayHello()
[/code]
|