Python Class: Object-oriented advanced programming multiple inheritance

Source: Internet
Author: User

Inheritance: The purpose for which subclasses can extend functionality by inheriting parent class information from subclasses

Multiple inheritance: Inheriting multiple classes through subclasses


One, multiple inheritance category

Examples of applying Liaoche:

Https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/ 0013868200511568dd94e77b21d4b8597ede8bf65c36bcd000

    • Dog-Dogs;

    • Bat-bat;

    • Parrot-Parrot;

    • Ostrich-Ostrich.



By Lactation and birds:

By running and flying points:


Total:


Program:

#!/usr/bin/python
#-*-Coding:utf-8-*-

Class Animal (object):
Pass


# Big class:
Class mammal (Animal):
Pass

Class Bird (Animal):
Pass


#功能
Class Runnable (object):
def run (self):
Print (' Running ... ')

Class Flyable (object):
Def fly (self):
Print (' Flying ... ')


# Various animals:
Class Dog (Mammal, Runnable):
Pass

Class Bat (Mammal, flyable):
Pass

Class Parrot (Bird, flyable):
Pass

Class Ostrich (Bird, Runnable):
Pass


Dog = Dog ()
Dog.run ()


Running Result: Running ...


Second, multiple inheritance two categories:Mixin

Except for large classes, other inherited classes, mixin is ready.

Program:


#!/usr/bin/python
#-*-Coding:utf-8-*-

Class Animal (object):
Pass


# Big class:
Class mammal (Animal):
Pass

Class Bird (Animal):
Pass


#功能
Class Runnablemixin (object):
def run (self):
Print (' Running ... ')

Class Flyablemixin (object):
Def fly (self):
Print (' Flying ... ')


# Various animals:
Class Dog (Mammal, runnablemixin):
Pass
Class Bat (Mammal, flyablemixin):
Pass
Class Parrot (Bird, flyablemixin):
Pass
Class Ostrich (Bird, runnablemixin):


Dog = Dog ()
Dog.run ()

Running Result: Running ...


Finally found that there is no difference between a class and two ... Tease me ... Mask it and look back later.



Python Class: Object-oriented advanced programming multiple 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.