クラス日付: #构造函数 デフ __init__ (自己、年、月、日): self.year = 年 self.month = 月の self.day = 日 デフ明日(自己): self.day + = 1 @staticmethod デフparse_from_string( date_str): 年、月、日 =タプル(date_str.split(" - " )) リターン日(int型(年)、INT(月)、INT(日)) @staticmethod デフvalid_str(date_str): 年、月、日タプル=(date_str.split(" - " )) IF int型(年)> 0 と(INT(月ザ・)> 0 と INT(月ザ・)<= 12である)、および(INT(日)> 0 と INT(日)< 31 = ): 返す真の 他: 返す偽 @classmethodの DEFのfrom_stringには(CLS、DATE_STR): 年、月ザ・、デイ =タプル(date_str.split(" - " )) 戻りCLS(int型(年)、INT(月ザ・)、 INT(日))#クラス名が変更されている場合は、ここで、それから、私たちはクラスのインスタンスに戻ったとき、クラス名には戻らないだろう DEF __str__(自己): リターン " {年} / {月} / {日} " .format(年= self.year、月= self.month、日= self.day) の場合 __name__ == " __main__ " : new_day =日(2018、12、31 ) new_day.tomorrow() プリント(new_day) #2018年12月31日 date_str = " 2018年12月31日" 年、月、日 =タプル(date_str.split(" - " )) new_day = 日付(int型(年)、INT(月)、INT(日)) プリント(new_day) #初期化は、staticmethodで完成さ new_day = Date.parse_from_string(DATE_STR) 印刷(new_day) #クラスメソッドの完全な初期化と new_day = Date.from_string(DATE_STR) 印刷(new_day) 印刷(Date.valid_str(" 2018-12-32 "))