程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How to implement class inheritance in Python

編輯:Python

Python How to implement class inheritance in

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 !

1. Single inheritance

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 .

2. Method rewriting in inheritance

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 :

  1. Methods that override the parent class

  2. 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 :

  1. Use... Where needed super(). Parent class method To call the execution of the parent method

  2. 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()

3. Multiple inheritance

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 !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved