@classmethod:装饰器。顾名思义,对原物件进行外部修饰,不做内里的改变。
classmethod是用来指定一个类的方法为类方法,即顶着@classmethod所定义的函数,其所传递参数可为一个类,而非仅限于一般格式的诸如int、boolean等类型的参数。
使用方法如下:
class C:
@classmethod
def f(cls, arg1, arg2, ...): ...
实例说明:
class Data_test(object):
def __init__(self,year=0,month=0,day=0):
self.day=day
self.month=month
self.year=year
def out_date(self):
print "year:"
print self.year
print "month:"
print self.month
print "day:"
print self.day
t = Data_test(2016,8,1)
t.out_date()
输出:
year:
2016
month:
8
day:
1
如果用户输入的是"2016-8-1" 这样的字符格式,那么就需要调用Date_test 类前