#In Python multi inheritance, if a subclass inherits a method with the same name in multiple parent classes, which subclass will choose when calling
class Item : def info (self): print("Item Method in","This is a commodity") class Product: def info(self): print("Product Method in" , "This is an industrial product") class Mouse(Item, Product): pass m = Mouse() m.info()
Output:
Method in Item, this is a product
class Item : def info (self): print("Item Method in","This is a commodity") class Product: def info(self): print("Product Method in" , "This is an industrial product") class Mouse(Product, Item): pass m = Mouse() m.info()
Output:
Method in Product this is an industrial Product
Conclusion:
It can be seen that in multiple inheritance, the parent class has a method with the same name, and the child class will select the method of the parent class ranking first in inheritance when calling
Expand:
If a subclass also contains a method with the same name, the subclass calls the method with the same name and chooses its own method first. This is called override, or override override
class Item : def info (self): print("Item Method in","This is a commodity") class Product: def info(self): print("Product Method in" , "This is an industrial product") class Mouse(Item, Product): def info(self): print("This is a mouse") m = Mouse() m.info()
Output:
This is a mouse
Expand 1:
If the subclass and the superclass method have the same name in multiple inheritance, and the superclass method is covered by the superclass method, but I still want to call the superclass method, what should I do?
Python classes are equivalent to class space, so methods in Python classes are essentially equivalent to functions in class space.
Therefore, Python allows calls through class names, even for instance methods. The difference is that when an instance method is called by class name, Python does not automatically bind the parameter value for the first parameter self of the instance method, but requires the program to explicitly bind the first parameter self. This mechanism is known as unbound methods.
Look at the code again:
# In Python multi inheritance, if a subclass inherits a method with the same name in multiple parent classes, which subclass will choose when calling class Item : def info (self): print("Item Method in","This is a commodity") class Product: def info(self): print("Product Method in" , "This is an industrial product") class Mouse(Item, Product): def info(self): print("This is a mouse") def all_method(self): self.info() #info method of subclass Item.info(self) #info method of Item parent class Product.info(self) #info method of Product parent class m = Mouse() m.all_method()
Output results:
This is a mouse
Method in Item this is a product
Method in Product this is an industrial Product
This means that all info methods are called through this method
Expand 2:
Using super function to call the construction method of parent class
Because the construction method is a special method built in Python, which is very common in classes, we must consider how the subclass calls the construction method of the parent class in multiple inheritance:
class Employee: def __init__(self, salary): self.salary = salary def work(self): print("Ordinary employees are writing codes. The salary is:", self.salary) class Customer: def __init__(self, favorite, address): self.favorite = favorite self.address = address def info(self): print("I'm a customer. My hobby is%s, The address is%s" %(self.favorite, self.address)) class Manger(Employee, Customer): pass m = Manger(5000) #The subclass inherits the construction method of the parent Employee m.work() # m.info() #Error because the construction property defined in Customer is used in info, but the subclass does not inherit the favorite, address property from Customer
So the construction method is very special because it initializes the properties for the object. These properties are used in other methods. If the init method of the parent class is not inherited in the subclass inheritance, some methods of the parent class will be reused. The biggest point of inheritance is reuse. If it cannot be reused, inheritance is meaningless. Therefore, we must rewrite the parent class in the subclass It inherits the init method of all the parent classes and also extends the properties of the child classes. There are two init methods that inherit the parent class:
- Using unbound methods is easy to understand. Because the constructor is also an instance method, it can be called in this way.
- Use the super() function to call the constructor of the parent class.
class Employee: def __init__(self, salary): self.salary = salary def work(self): print("Ordinary employees are writing codes. The salary is:", self.salary) class Customer: def __init__(self, favorite, address): self.favorite = favorite self.address = address def info(self): print("I'm a customer. My hobby is%s, The address is%s" %(self.favorite, self.address)) # class Manager(Employee, Customer): # pass # # m = Manager(5000) #The subclass inherits the construction method of the parent Employee # m.work() # # m.info() #Error because the construction property defined in Customer is used in info, but the subclass does not inherit the favorite, address property from Customer class Manager(Employee, Customer): def __init__(self, salary, favorite, address, occupation): #To override init, you need to write the init attribute in all the parent classes, and at the same time, you can add the attribute allocation of the child classes self.occupation = "new add" print("This is Manager Construction method of class") # Inherit init method and super method in Employee, both of which can be written # super().__init__(salary) #super writing 1 super(Manager, self).__init__(salary) #super writing 2 # Inherit the Customer method and explicitly pass self by calling the unbound method Customer.__init__(self, favorite, address) print("I am a subclass. Manager Initialization method init Properties defined in:", self.occupation) m = Manager(5000, 'sing', 'china', 'IT') m.work() m.info()