This article mainly explains “Python How to implement class inheritance in ”, Interested friends might as well come and have a look . The method introduced in this paper is simple and fast , Practical . Now let Xiaobian take you to learn “Python How to implement class inheritance in ” Well !
The main function of inheritance is to realize code reuse . Inheritance enables subclasses to have methods and properties of the parent class .
Let's just take a look at an example .
class animal: def eat(self): print(" eat ") def drink(self): print(" drink ") class dog(animal): def dark(self): print(" bark ") goudan = dog()goudan.eat()goudan.drink()
As you can see from the code above , Writing dog Class time , We didn't rewrite eat and drink Two methods . All we need to do is dog Add the name of the parent class in the following brackets . When a subclass inherits the parent class , Subclasses can directly use the methods in the parent class . In this case ,goudan You can use it directly animal Class eat and drink Two methods .
And a magical thing is , Inheritance is transitive . Subclasses have the methods and properties of the parent class , It also has the attributes of the parent class of the parent class .
occasionally , Methods in the parent class cannot fulfill the needs of the child class . At this point, we need to rewrite the method . There are two general situations for subclass method overrides :
Methods that override the parent class
Extend the method of the parent class
A simple way is to rewrite the corresponding method directly . The following example is rewritten drink This method . When using goudan call drink This method is , The result is not to drink but to drink water with your tongue .
class animal: def eat(self): print(" eat ") def drink(self): print(" drink ") class dog(animal): def drink(self): print(" Drink water with your tongue ") def dark(self): print(" bark ") goudan = dog()goudan.eat()goudan.drink()
When our requirement is not to simply override the methods in the parent class , Instead, the method is extended on the basis of the parent method . Here's how :
Use... Where needed super(). Parent class method To call the execution of the parent method
Other parts of the code address the need for subclasses , Write code that subclasses hold to implement
Let's take an example , here goudan Calling drink Method time , I can drink and drink water with my tongue .
class animal: def eat(self): print(" eat ") def drink(self): print(" drink ") class dog(animal): def drink(self): # 1. Write specific code according to the needs of subclasses print(" Drink water with your tongue ") # 2. Use super Inherit from the parent class fangfa super().drink def dark(self): print(" bark ") goudan = dog()goudan.eat()goudan.drink()
about Python for , A subclass can have more than one parent class . The basic syntax of multiple inheritance is relatively simple . Don't talk much , Let's look at an example
class A(): def test(self): print("test") class B(): def demo(self): print("demo")class C(A,B): passc = C()c.test()c.demo()
Here we are , I'm sure you're right “Python How to implement class inheritance in ” Have a deeper understanding of , You might as well put it into practice ! This is the Yisu cloud website , For more relevant contents, you can enter the relevant channels for inquiry , Pay attention to our , Continue to learn !