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

Easy to understand class inheritance in Python

編輯:Python

Catalog

1. Single inheritance

2. Method rewriting in inheritance

3. Multiple inheritance

summary

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 :

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 fangfasuper().drinkdef 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() summary

This is about Python This is the end of the article on class inheritance in , More about Python Class inheritance content please search the previous articles of software development network or continue to browse the following related articles. I hope you can support software development network in the future !



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