First-entry Python inheritance

Source: Internet
Author: User

1. What is inheritance?

New classes don't have to be written from scratch

The new class inherits from the existing class and automatically has all the functionality of the existing class

New classes only need to write new features that are missing from existing classes

2. Benefits of Inheritance

Reusing existing code

Automatically has all the features of an existing class

You just need to write the missing new features

3. Parent and Child classes

1 #defines a parent class, also known as a base class, a superclass2 classfather (object):3     def __init__(self,name,gender):4Self.name =name5Self.gender =Gender6 7 #defines a subclass, also known as a derived class, that inherits a class8 classChild (father):9     def __init__(self,name,gender,score):Ten         #initializes the parent class, otherwise the child inheriting from father will have no name and gender OneSuper (Child,self).__init__(Name,gender) ASelf.score =score -  -t = Child ('Tom','Six','98') the PrintT.name

4. Characteristics of inheritance

Subclasses and parent classes are the IS relationships

class Child (father):     Pass  == Child ()

P is a father but not a child

S is a father and also a person

First-entry Python 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.