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

Example Analysis of combination and inheritance of python

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains the "python combination and inheritance example analysis", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "python combination and inheritance example analysis" bar!

Combination

The way of object-oriented programming code reuse is not only inheritance but also composition, which means that a part of the data in a class is an instance (object) of another class.

Case study:

There are two classes, which are circles and rings, written in a combined way.

The following are the circle classes:

Class Circle:pi = 3.14159265358979def _ init__ (self, r)-> None:self.r = r @ propertydef area (self): return self.pi*self.r**2@propertydef perimeter (self): return self.pi*self.r*2

The following are the torus classes:

Class Ring:def _ init__ (self, inner, outer): inner, outer = (inner, outer) if inner < outer else (outer, inner) self.inner = Circle (inner) self.outer = Circle (outer) @ propertydef area (self): return self.outer.area-self.inner.area @ propertydef perimeter (self): return self.outer.perimeter+self.inner.perimeter

Note:

The init function of the ring class, whose parameters inner and outer are the radius of the circle, first judge the size of the two parameters inner and outer in the code block, ensure that the large values are assigned to outer and the small values are assigned to inner, and then use inner and outer as parameters to call the Circle class to create inner instances and outer instances. So two properties in creating an instance of the Ring class are instances of the Circle class.

Both the area method and the perimeter method call the corresponding method in Circle for calculation.

Summary:

The combination method can improve the code reuse rate, and only a small amount of code needs to be changed in the future when the requirements change (for example, changing the accuracy of pi, only 2 digits after the decimal point), which reduces the probability of error and improves the maintainability of the code.

Inherit

Inheritance is a way to define classes, and subclasses can reuse the data and code blocks of the parent class through inheritance.

There is a very important hidden data in the subclass-the class pointer, which accesses the subclass. Variable (the variable points to the data that it is the attribute, and points to the code block when it is the method). If there is no such variable in the subclass, the interpreter will access the parent class through the class pointer. If there is no such variable in the parent class, it will continue to access the grandfather class. If access to the ancestor object class of all classes (all classes in python inherit from the object class), the interpreter will report an error. Note: the class pointer is one-way! It means that the subclass can access the variables of the parent class, while the parent class cannot access the variables of the subclass.

In addition: python supports multiple inheritance, while java only supports single inheritance.

Add: the most important thing in learning inheritance is to figure out the process of class pointers, different classes of namespaces and instance namespaces to create and find variables. if you want to learn more, you'd better read the code and draw a memory diagram by yourself.

Case study:

Class Animal:kind = 'animal' def _ init__ (self, name): self.name = namedef eat (self): print (f'{self.name} is eating!') Class Cat (Animal): def _ _ init__ (self, name, count=0): self.count = count Animal.__init__ (self Name) def mousing (self): print (f 'cat "{self.name}" caught {self.count} mice') white_cat = Cat ('big white cat') white_cat.count + = 1white_cat.mousing () white_cat.eat () print (white_cat.kind) out: cat "big white cat" caught a mouse and big white cat is eating! Animal

Explanation:

The above is a simple case of inheritance, and I'll sort it out in detail through this case:

First, define the parent class, which is Animal. In the body of the class, there are class pointers and kind properties to the object class, _ _ init__ methods and eat methods, which are common properties and methods for all animals. When writing code, the parent class code should be placed in front of the subclass code.

Then define the subclass, and define the subclass Cat with the parent class Animal as a parameter, which means that the subclass inherits the parent class. In the body of the class, there are class pointers to the Animal class, the _ _ init__ method, and the mousing method. The count property (the number of mice caught) is initialized in the _ _ init__ method, and the rest are initialized by calling the _ _ init__ method of the parent class.

Next, create the instance big white cat, first open up a namespace when creating the instance, create a class pointer to the Cat class in this memory space, then pass the address of the memory space (the address is called self) to the _ _ init__ method of the Cat class, create the self.count attribute, and then call the _ _ init__ method of the parent class to create the self.name attribute. So the namespace of the instance "Big White Cat" includes the class pointer to the Cat class, the name attribute, and the count attribute.

Note: the properties of the instance are different, so the properties are placed in the instance's own namespace. All instances of a class call the same methods, so the methods of the instances are placed in the namespace of the class to which they belong!

Thank you for your reading, the above is the content of "python combination and inheritance example Analysis". After the study of this article, I believe you have a deeper understanding of the combination and inheritance example analysis of python, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Internet Technology

Wechat

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

12
Report