Python class definition Inheritance

Source: Internet
Author: User

1 defining a time class with default parameters
classMyTime (object):def __init__(self,hour=21, Minute=13, Second=50): Self.hour=hour Self.minute=minute Self.second=Seconddef __str__(self):return '__str__ Hour:minute:second =%02d:%02d:%02d'                %(Self.hour,self.minute,self.second)defGet_hour (self):returnSelf.hourdefGet_minute (self):returnSelf.minutedefGet_second (self):returnSelf.seconddefPrint_time (self):Print  'Hour:minute:second =%02d:%02d:%02d'%(Self.hour,self.minute,self.second)
Description: Constructor__init__ (self,hour=21, minute=13, second=50) with the default parameters defined way; function __str__ for output a method for obtaining a property value Get_xxxxmethods for outputting attribute values print_time 2 Defining a DateTime class, inheriting the properties and methods of the time class
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" C12/>def print_date (self):        print  ' year-mounth-day =%04d-%02d-%02d '         % (Self.year,self.mounth, Self.day) #        Self.print_time ()  
Description: Mydatetime inherits the MyTime property value, here Mydatetime (subclass), MyTime (parent class) constructor__init__ (self,year = 2016,mounth = 1,day = 1, hour=21, minute=13,second=50) is defined with default parameters, note that the Parent property value is initialized by super; function __str__ for output methods for outputting attribute values print_date 3 Testing
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
Test output
>>> runfile (' f:/python/hysrc_py/untitled0_class.py ', wdir=r ' f:/python/hysrc_py ')--------------------  mytime t0:__str__ hour:minute:second = 21:13:50hour:minute:second = 21:13:5021--------------------  mytime T1 : Hour:minute:second = 07:13:20--------------------  

Python class definition Inheritance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.