用法一:classmethod 和 staticmethod 都可以不用實(shí)例化直接調(diào)用
class demo1:
@classmethod
def c_demo(cls):
return 1
@staticmethod
def s_demo():
return 2
print(demo1.c_demo()) #1
print(demo1.s_demo()) #2
用法二:在不改變已經(jīng)寫好的類里面的方法的情況下,對(duì)輸入的數(shù)據(jù)進(jìn)行處理噪裕,在外國(guó)論壇看到一個(gè)特別好的例子
# https://stackoverflow.com/questions/12179271/meaning-of-classmethod-and-staticmethod-for-beginner
# 輸出年月日株婴,正常的情況下
class demo2:
def __init__(self, year = 0, month = 0, day = 0):
self.year = year
self.month = month
self.day = day
def out_date(self):
return "year:%d, month:%d, day:%d" % (self.year, self.month, self.day)
year = 2017
month = 7
day = 1
demo2 = demo2(year, month, day)
print(demo2.out_date())
# year:2017, month:7, day:1
# 如果用戶輸入的是2017-5-6格式怎虫,需要在輸出前處理一下,就可以使用classmethod達(dá)到想要的效果
class demo3:
def __init__(self, year = 0, month = 0, day = 0):
self.year = year
self.month = month
self.day = day
def out_date(self):
return "year:%d, month:%d, day:%d" % (self.year, self.month, self.day)
@classmethod
def pre_out(cls, date_string):
year, month, day = map(int, date_string.split("-"))
return cls(year, month, day)
date = "2017-5-6"
year = 2017
month = 7
day = 1
try:
demo3 = demo3.pre_out(date)
except:
demo3 = demo3(year, month, day)
print(demo3.out_date())
# year:2017, month:5, day:6