Python Basic (11) Class inheritance

Source: Internet
Author: User

Class Inheritance:

The idea of inheritance is to fully leverage the functionality of existing classes and extend them to define new classes on their basis.

Parent class (parents) and child class (subclass):

The inherited class is called the parent class, and the inherited class is called a subclass, and a parent class can have more than one child class;

Subclasses, once inheriting the parent class, have the properties and methods of the parent class, which can be added and changed as needed.

One of the main benefits of this approach is code reuse.

Example code 1:

#*_*coding:utf-8*_*classPerent_class (object):def __init__(self,name): Self.name=namedefPrintname (self):Print('your name is:'+self.name)classChild_class (perent_class):def __init__(Self,name,age): Super (child_class,self).__init__(name)#initializes the class super () function to return the class objectSelf.age = Ageif __name__=="__main__": Child_class ('Xzdylyh',' -'). Printname ()

Output

C:\Python27\python.exe d:/weixin/temp/temp.py Your name is: Xzdylyhprocess finished with exit code 0

When a subclass is in the same way as a parent class, the subclass overrides the method content of the parent class, which is, in fact, a rewrite and polymorphism.

Python Basic (11) Class inheritance

Related Article

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.