A brief introduction to inheritance (single inheritance, multiple inheritance) in a program

Keywords: Attribute

Inheritance in program

  • In a program, inheritance describes the ownership of multiple classes.
  • If the properties and methods in class A can be reused, they can be passed to class B through inheritance.
  • Then class A is the base class, also called the parent class; class B is the derived class, also called the child class.

Single inheritance: a subclass inherits only one parent

  • Story: pancake fruit teacher Fu has been in the pancake fruit world for decades, with a superb pancake fruit technology, and summed up a set of "ancient pancake fruit formula".

  • But the teacher Fu is old and hopes to pass on his formula before burping farts, so the teacher Fu passes it on to his apprentice big cat

# Define a Master class
class Master(object):
    def __init__(self):
        # attribute
        self.kongfu = "Recipe of ancient pancake and fruit" 

    # Instance method
    def make_cake(self):
        print("according to <%s> Made a pancake fruit..." % self.kongfu)


# Define Prentice class. If Master is inherited, Prentice is a child class and Master is a parent class.
class Prentice(Master): 
    # A child class can inherit all the properties and methods of the parent class. Even if the child class does not have its own properties and methods, it can also use the properties and methods of the parent class.
    pass                

# laoli = Master()
# print(laoli.kongfu)
# laoli.make_cake()

damao = Prentice()  # Create subclass instance object
print(damao.kongfu) # Subclass objects can directly use the properties of the parent class
damao.make_cake()   # Subclass objects can directly use the methods of the parent class

explain:

  • Although the subclass does not define the init method initialization property or the instance method, the parent class does. So as long as the subclass object is created, the inherited init method is executed by default

Conclusion:

  • When a subclass inherits and defines a class, the name of the parent class is in parentheses ()
  • The properties and methods of the parent class will be inherited to the child class

Multiple inheritance: children inherit multiple parents

class Master(object):
    def __init__(self):
        self.kongfu = "Recipe of ancient pancake and fruit"  # Instance variables, properties

    def make_cake(self):                    # Instance method
        print("[time-honored methods] according to <%s> Made a pancake fruit..." % self.kongfu)

    def dayandai(self):
        print("Master's big cigarette bag..")

class School(object):
    def __init__(self):
        self.kongfu = "Modern pancake fruit formula"

    def make_cake(self):
        print("[modern] according to <%s> Made a pancake fruit..." % self.kongfu)

    def xiaoyandai(self):
        print("School cigarette bags..")

# class Prentice(School, Master):  # Multiple inheritance, inherited multiple parent classes (School before)
#     pass

# damao = Prentice()
# print(damao.kongfu)
# damao.make_cake()
# damao.dayandai()
# damao.xiaoyandai()


class Prentice(Master, School):  # Multiple inheritance, inherited multiple parent classes (Master first)
    pass

damao = Prentice()
print(damao.kongfu) # Execute properties of Master
damao.make_cake() # Instance method of executing Master

# The magic properties of subclasses determine the search order of properties and methods
print(Prentice.__mro__)

damao.dayandai() # No duplicate name will not be affected
damao.xiaoyandai()

explain:

  • Multiple inheritance can inherit multiple parent classes, as well as the properties and methods of all parent classes
  • Note: if there are properties and methods with the same name in multiple parent classes, the properties and methods of the first parent class are used by default (based on the order of the magic properties mro of the class)
  • In multiple parent classes, properties and methods that do not have the same name have no effect.

Posted by webproclaim on Sun, 22 Dec 2019 06:52:21 -0800