In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "inheritance and Polymorphism in python". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
I. introduction of inheritance
Inheritance is a way to create new classes. Newly created classes are called subclasses, and inherited classes are called parent classes, superclasses, and base classes. The characteristic of inheritance is that subclasses can use the attributes (characteristics, skills) of the parent class. Inheritance is the relationship between classes.
Inheritance can reduce code redundancy and improve reusability.
In real life, inheritance generally refers to children inheriting the property of their parents, as shown in the following figure:
Inheritance in Python and inheritance and Polymorphism in Polymorphic Python II. How to use inheritance?
1. Inheritance syntax
Class derived class name (base class name): # base class name is written in parentheses.
In inheritance relationships, existing, designed classes are called parent or base classes, and newly designed classes are called subclasses or derived classes. A derived class can inherit the public members of the parent class, but not its private members.
two。 The characteristics of inheritance
The constructor (init () method) of the base class is not called automatically in inheritance; it needs to be specifically called in person in the construction of its derived class.
If you need to call the method of the base class in a derived class, it is done by the method of "base class name. method name ()", which needs to be prefixed with the class name of the base class and the self parameter variable. It is different from calling a normal function in a class without taking the self parameter. You can also use the built-in function super () to do this.
Python always looks for the corresponding type of method first, and if it can't find the corresponding method in the derived class, it starts to look in the base class one by one (look for the called method in this class first, and then go to the base class if you can't find it).
3. Single inheritance
Example:
Class Animal: # parent def eat (self): print ("- eat -") def drink (self): print ("- drink -") class Dog (Animal): # subclass inherits parent "def eat (self): print ("-eat-") def drink (self): print ("-- -drink-")"passclass Cat: passwang_cai = Dog () wang_cai.eat () wang_cai.drink ()
Running result:
Multi-tier inheritance
Example:
Class Animal: def eat (self): print ("- eat -") def drink (self): print ("- drink -") class Dog (Animal): def bark (self): print ("- bark -") class XTQ (Dog): "" defines a "passclass Cat (Animal)" : def catch (self): print ("- catching mice -") xtq = XTQ () xtq.eat () xtq.bark ()
Running result:
Override parent class methods
Example:
Class Animal: # parent def eat (self): print ("- eat -") def drink (self): print ("- drink -") class Dog (Animal): def bark (self): print ("- bark -") class XTQ (Dog): # rewrite Dog method "" defines a howling sky. Dogs "" def bark (self): print ("- cry -") class Cat (Animal): def catch (self): print ("- catch mice -") xtq = XTQ () xtq.eat () xtq.bark ()
Running result:
4. Multiple inheritance
4.1 multiple inheritance
As can be seen from the figure, the so-called multi-inheritance means that subclasses have multiple parent classes and have their characteristics.
The format of multiple inheritance in Python is as follows:
# define a parent class, class A: def printA (self): print ('- A Merry class') # define a parent class, class B: def printB (self): print ('- BMMI Mermel') # define a subclass Inheriting from A, Bclass C (Amam B): def printC (self): print ('- Cmurmuri Murray') obj_C = C () obj_C.printA () obj_C.printB ()
Running result:
-A-A
Multiple inheritance is possible in Python, and methods and properties in the parent class will be inherited by the subclass.
Think about it:
If in the above multi-inheritance example, if there is a method with the same name in parent class An and parent class B, which method will be called when it is called through the child class?
# coding=utf-8class base (object): def test (self): print ('- base test----') class A (base): def test (self): print ('- A test----') # define a parent class class B (base): def test (self): print ('- B test----') # define a subclass Inherit from A, Bclass C (A Magi B): passobj_C = C () obj_C.test () print (C. searching methods _) # you can view the sequence of object search methods in class C.
Running result:
5. Polymorphisms
5.1 what is polymorphism?
The concept of polymorphism is applied to strongly typed languages such as Java and C #, while Python advocates the "duck type".
The so-called polymorphism: when the type of definition is different from that of the runtime, it becomes polymorphic.
Python pseudocode implements the polymorphism of Java or C#.
5.2 case
Python "duck type"
Class Duck: def quack (self): print ("Quaaaaaack!") class Bird: def quack (self): print ("bird imitate duck.") class Doge: def quack (self): print ("doge imitate duck.") def in_the_forest (duck): duck.quack () duck = Duck () bird = Bird () doge = Doge () for x in [duck, bird, doge]: in_the_forest (x)
Running result:
That's all for "inheritance and Polymorphism in python". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.