class Date: #构造函数 def __init__(self, year, month, day): self.year = year self.month = month self.day = day def tomorrow(self): self.day += 1 @staticmethod def parse_from_string(date_str): year, month, day = tuple(date_str.split("-")) return Date(int(year), int(month), int(day)) @staticmethod def valid_str(date_str): year, month, day Tuple = (date_str.split ( " - " )) IF int (year)> 0 and (int (month The)> 0 and int (month The) <= 12 is) and (int (Day)> 0 and int (Day) < 31 = ): return True the else : return False @classmethod DEF from_string (CLS, DATE_STR): year, month The, Day = tuple (date_str.split ( " - " )) return CLS (int (year), int (month The), int (day)) # here If the class name has changed, then, when we return to class instance, would not return to the class name of the DEF __str__(self): return "{year}/{month}/{day}".format(year=self.year, month=self.month, day=self.day) if __name__ == "__main__": new_day = Date(2018, 12, 31) new_day.tomorrow() print(new_day) #2018-12-31 date_str = "2018-12-31" year, month, day = tuple(date_str.split("-")) new_day = Date(int(year), int(month), int(day)) print (new_day) #Initialization is completed with staticmethod new_day = Date.parse_from_string (DATE_STR) Print (new_day) # with classmethod complete initialization new_day = Date.from_string (DATE_STR) Print (new_day) Print (Date.valid_str ( " 2018-12-32 " ))