Explain Word Python inherits the parent class _

1. The first question:

  We know that the class can inherit from other classes, in the succession process, we not only inherit the parent class's method can also inherit the properties of the parent class, you can also add your own stuff on the basis of the parent class.

 

2. The second question:

  We inherit the properties and methods of the parent class time without then the properties and methods of the parent class to write in general. The method is generally used: the name of the parent class methods / properties, there is a way to further super () method. But in fact these two methods is something different.

  First, the first way of example: the name of the parent class method / property.

  Code:

class BaseClass:
    num_Base_calls = 0
    def call_me(self):
        print("Calling method on BaseClass")
        self.num_Base_calls += 1

class LeftSubclass(BaseClass):
    num_left_calls = 0
    def call_me(self):
        BaseClass.call_me(self)
        print("Calling method on LeftSubclass")
        self.num_left_calls += 1

class RightSubclass(BaseClass):
    num_Right_calls = 0
    def call_me(self):
        BaseClass.call_me(self)
        print("Calling method on RightSubclass")
        self.num_Right_calls += 1

class Subclass(LeftSubclass,RightSubclass):
    num_Sub_calls = 0
    def call_me(self):
        LeftSubclass.call_me(self)
        RightSubclass.call_me(self)
        print("Calling method on Subclass")
        self.num_Sub_calls += 1

s = Subclass()
s.call_me()

# Calling method on BaseClass
# Calling method on LeftSubclass
# Calling method on BaseClass
# Calling method on RightSubclass
# Calling method on Subclass

print(s.num_Sub_calls)
print(s.num_left_calls)
print(s.num_Right_calls)
print(s.num_Base_calls)

# 1
# 1
# 1
# 2

  Note 1: There is such a base class called Baseclass, to inherit Leftclass and Rightclass, then Leftclass and Rightclass respectively inheritance to Subclass. Is such a simple relationship.

  Note 2: The problem is, when the last Subclass executed, we found Call_me most basic method of the base class is executed twice?

  Note 3: For multiple inheritance to remember is that we want the next level of the class method invocation, rather than the "parent" approach. In fact, the next method may not belong to the class of the parent class or earlier ancestors.

  4. DESCRIPTION said point is more aware of this model is shown in FIG:

  subclass - looking --Leftclass-- looking --Baseclass: first pass: print Baseclass and Leftclass

  subclass - looking - Rightclass - looking - Baseclass: second time: Print: Baseclass and Rightclass

  subclass - own - Print subclass

  Note 5: In fact, we are actually in the process of succession in the end, we just want to find again the parent of the parent class (the original ancestor class), in this way it is not acceptable. So we introduced the super () function to look block only once.

  

  Second: We use super () method only to find it again ancestor class, as follows:

class BaseClass:
    num_Base_calls = 0
    def call_me(self):
        print("Calling method on BaseClass")
        self.num_Base_calls += 1

class LeftSubclass(BaseClass):
    num_left_calls = 0
    def call_me(self):
        super().call_me()
        print("Calling method on LeftSubclass")
        self.num_left_calls += 1

class RightSubclass(BaseClass):
    num_Right_calls = 0
    def call_me(self):
        super().call_me()
        print("Calling method on RightSubclass")
        self.num_Right_calls += 1

class Subclass(LeftSubclass,RightSubclass):
    num_Sub_calls = 0
    def call_me(self):
        super().call_me()
        print("Calling method on Subclass")
        self.num_Sub_calls += 1

s = Subclass()
s.call_me()

# Calling method on BaseClass
# Calling method on RightSubclass
# Calling method on LeftSubclass
# Calling method on Subclass

print(s.num_Sub_calls)
print(s.num_left_calls)
print(s.num_Right_calls)
print(s.num_Base_calls)

# 1
# 1
# 1
# 1

  Note 1: We have added super in each class in each sub-category () in this way to solve this problem.

  Note 2: Thus the search order becomes as follows:

  subclass - Find - leftclass and rightclass - contain - Baseclass: Print: basecalss, right subclass, leftclass, subclass

 

3. Summary:

  The first method is to use the binary mode path, to go again, go again looped back.

  The second method is to use a contained way, like peeling an onion, layer by layer peeling, no similar reservations.

  

 

Guess you like

Origin www.cnblogs.com/noah0532/p/10958282.html