Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Single inheritance and multiple inheritance usage in Python classes

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly talks about "single inheritance and multi-inheritance usage in Python class". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the usage of single inheritance and multiple inheritance in the Python class.

Preface

Python object-oriented programming language, there are List, Str, Dict, Tuple and other data types, these data types are also objects, such as List class can have count method, we can use the count method to count the number of elements in the list.

We can simply define the definition class to understand that the achievement is to define the data structure, while developing some unique properties or methods for this data structure. Let's first look at the simplest class, where we define the Point class

# in order to display the variables from IPython.core.interactiveshell import InteractiveShellInteractiveShell.ast_node_interactivity = 'all'class Point (object): def _ _ init__ (self, xchang0, yroom0): self.x = x self.y = y def String (self): # display the information of Point return "{X:" + str (self.x) + ", Y:" + str (self.y) + "}"

We need to instantiate the class before calling it. This can be understood as a painting painted by Ma Liang. If it is not invigorated, it will always be a painting. By instantiating and invoking the instance, the object in the picture is alive.

# instantiate the Point class point = Point (xan3, yuan4) # display the point object point # display the String method point.String () of the point

Result of code running

'{XRO 3, YRO 4}'

Look at the profile.

Single inheritance

Just inherited a base class. Now we define a Circle class where the circle is determined by the center and radius, so Circle inherits and uses some of the features of Point.

Class Circle (Point): def _ init__ (self, x, y, radius=0): # Circle inherits and uses Point's _ _ init__ function Point.__init__ (self, x, y) # different from Point's parameter radius self.radius = radius def String (self): # displays the information return Point.String (self) of Circle + " {Radius: "+ str (self.radius) +"} "

Now let's take a look at Circle.

# instantiate the Circle class circle = Circle (xan3, yuan2, radius=5) # display circle object information circle# display circle String method circle.String ()

Result of code running

'{XRNAL 3, YRU 2}, {Radius:5}' multiple inheritance

Multiple inheritance, as the name implies, means that a class inherits multiple parent classes (base class)

Class Size (object): def _ init__ (self, width, height): self.width = width self.height = height def String (self): return "{Width:" + str (self.width) + ", Height:" + str (self.height) + "}"

Go ahead

# instantiate Size object size = Size (width=5, height=3) sizesize.String ()

Running result

'{Width: 5, Height: 3}'

You now have the Point, Size classes, and the String () method in each class. Now you want to build the square Rectangle class. In order for Rectangle to have some of the features of Point and Size, you need to have Rectangle inherit some of the unique methods of Point and Size.

Class Rectangle (Point, Size): def _ _ init__ (self, x, y, width, height): # Let Rectangle use the init method in Point so that Rectangle itself contains x and y attribute values Point.__init__ (self, x, y) # Let Rectangle use the init method in Size So that Size itself contains width and height attribute values Size.__init__ (self, width, height) def String (self): return "Rectangle's init point is" + Point.String (self) + " Size is "+ Size.String (self)

Rectangle inherits the Point and Size classes so that Rectangle does not have to define x, y, width, and height. And the String method of Rectangle can directly call the String method of Point and Size objects.

# instantiate Rectanglerect = Rectangle (x, y, width=5, height=3) # call rectrect# to display its method rect.String ()

Running result

"Rectangle's init point is {Width: 5, Height: 3}" so far, I believe you have a deeper understanding of "single inheritance and multi-inheritance usage in the Python class". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report