标签:
class mytime(object): def __init__(self,hour=21, minute=13, second=50): self.hour = hour self.minute = minute self.second = second def __str__(self): return ‘__str__ hour:minute:second = %02d:%02d:%02d‘ %(self.hour,self.minute,self.second) def get_hour(self): return self.hour def get_minute(self): return self.minute def get_second(self): return self.second def print_time(self): print ‘hour:minute:second = %02d:%02d:%02d‘ %(self.hour,self.minute,self.second)
class mydatetime(mytime): def __init__(self,year = 2016,mounth = 1,day = 1, hour=21, minute=13,second=50): super(mydatetime,self).__init__(hour,minute,second) self.year = year self.mounth = mounth self.day = day def __str__(self): return ‘__str__ year--mounth--day = %02d--%02d--%02d‘ %(self.year,self.mounth,self.day) def __del__(self): """ __del__ mydatetime destroyed """ print "__del__ mydatetime destroyed" def print_date(self): print ‘year-mounth-day = %04d-%02d-%02d‘ %(self.year,self.mounth,self.day) # self.print_time()
if __name__ == ‘__main__‘: print "\n" print "--"*10," mytime t0:" t0 = mytime() print t0 t0.print_time() print t0.get_hour() print "\n" print "--"*10," mytime t1:" t1 = mytime(hour=7,second=20) t1.print_time() print "\n" print "--"*10," mydatetime da:" da = mydatetime(minute=40) print da # da.print_date() da.print_time() del da
>>> runfile(‘F:/Python/hysrc_py/untitled0_class.py‘, wdir=r‘F:/Python/hysrc_py‘) -------------------- mytime t0: __str__ hour:minute:second = 21:13:50 hour:minute:second = 21:13:50 21 -------------------- mytime t1: hour:minute:second = 07:13:20 -------------------- mydatetime da: __str__ year--mounth--day = 2016--01--01 hour:minute:second = 21:40:50 __del__ mydatetime destroyed >>>
标签:
原文地址:http://www.cnblogs.com/hythink/p/5838253.html