In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Python MixIn model is how, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
On the Mixin mode of Python
Languages like C++ support multiple inheritance, and a subclass can have more than one parent class, which is often criticized. Because inheritance should be a "is-a" relationship. For example, cars inherit vehicles because cars are a ("is-a") means of transportation. An item cannot be many different things, so there should be no multiple inheritance.
But is there a situation where a class really needs to inherit multiple classes?
The answer is yes, let's take the means of transportation as an example. Civil aircraft is a means of transportation, and helicopters are also a means of transportation for tuhao. For both vehicles, they have a function of flying, but the car does not. Therefore, it is impossible for us to write the flight function in the parent class of vehicles.
But if civil aircraft and helicopters each write their own flight methods, it violates the principle of code reuse as much as possible (if there are more and more flight tools in the future, there will be a lot of duplicate code). What to do, we have to let these two kinds of aircraft inherit the two parent classes of vehicles and aircraft at the same time, so there is multiple inheritance.
At this point, it violates the inheritance must be a "is-a" relationship. How to solve this problem?
Different languages offer different approaches, so let's take a look at Java first. Java provides interface interface functionality to implement multiple inheritance:
Public abstract class Vehicle {
}
Public interface Flyable {
Public void fly ()
}
Public class FlyableImpl implements Flyable {
Public void fly () {
System.out.println ("I am flying")
}
}
Now our aircraft has both vehicle and aircraft properties, and we don't need to rewrite the flight methods in the aircraft, and we don't break the principle of single inheritance. The aircraft is a means of transportation, but the ability to fly is the attribute of the aircraft, which is obtained by inheriting the interface.
Public class Airplane extends Vehicle implements Flyable {
Private flyable
Public Airplane () {
Flyable = new FlyableImpl ()
}
Public void fly () {
Flyable.fly ()
}
}
Back to the topic, the Python language doesn't have interface functionality, but it can inherit multiple things. Should Python be implemented with multiple inheritance? Yes and no.
Yes, because grammatically, it is indeed achieved through multiple inheritance. Say no, because its inheritance still follows the "is-a" relationship, and in meaning it still follows the principle of single inheritance. How do you understand this? Let's look at examples.
Class Vehicle (object):
Pass
Class PlaneMixin (object):
Def fly (self):
Print'I am flying'
Class Airplane (Vehicle, PlaneMixin):
Pass
As you can see, the above Airplane class implements multiple inheritance, but the second class it inherits is named PlaneMixin, not Plane, which does not affect functionality, but will tell people who read the code later that this class is a Mixin class.
So in terms of meaning, Airplane is just a Vehicle, not a Plane. This Mixin, which means mix-in, tells others that this class is added to a subclass as a function, not as a parent class, and its function is the same as the interface in Java.
Using Mixin classes to implement multiple inheritance should be very careful
First of all, it must represent a certain function, not an item, like Runnable,Callable in Java, etc. Secondly, it must have a single responsibility, if there are multiple functions, then write multiple Mixin classes, and then, it does not depend on the implementation of the subclass. Finally, the subclass can still work even if it does not inherit the Mixin class, but lacks a certain function. Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.