Polymorphism required by javascript every day, javascript Polymorphism
Dear friends, today we will talk about the previous content. We have already talked about inheritance. Today we are talking about the last embodiment of OOP, namely polymorphism, because of the flexibility of the javascript language, therefore, we have no way to use interfaces, so this also brings some confusion to the js program, and you do not have to worry too much about this issue, because the Versions later than ECMAScript will solve these problems for us, and they will be far away. They will still return to the topic, OOP polymorphism, we can now clearly understand what inheritance looks like, that is, declare a parent class first, and then we can write many child classes to inherit the attributes and methods of the parent class, we can use the minimum amount of code to implement the same functions as the parent class. This is inheritance. I have been reading the inheritance for a long time and can understand it. However, I don't know why the primary node is used. Why do we need to write so many inheritance classes? This is a very important question. If there is no polymorphism, the inheritance we mentioned previously is useless, because all of our inheritance classes are identical copies, no features. For example:
We can clearly see the problem. pandatv A and pandatv B are exactly the same as their parent pandatv. Although we can easily write many sub-classes, this is useless. We can directly use the attributes and methods of the parent class to achieve our goal. Right, in this way, we feel that the inheritance of OOP is useless, the knowledge we learned before will not be wasted. Haha, don't be afraid. As long as you have learned something, you have never learned it in vain, next we will discuss the supplement of inheritance, polymorphism, and look at the example diagram:
Whether it is human beings or other animals, they constantly multiply and evolve. Each descendant looks the same and actually different. They all have their own unique attributes or behaviors, at present, pandatv A has learned to take A bath under certain special circumstances. It has its own hygiene attributes, while pandatv B is A master and has learned to pick up girls (I did not know it, I learned it ), it has its own charm attribute, so we can clearly see that when we need to write a subclass to inherit the parent class, it must have the same behavior or attribute as the parent class and its own unique behavior or attribute, in this way, code writing with the same behavior or attribute is saved (the benefits brought by OOP are reflected again). Let's look at the example code:
// Genetic inheritance function Extend (Children, Parent) {for (var p in Parent) {if (typeof Children [p] = "undefined ") {Children [p] = Parent [p] ;}}// function Panda () {// color this. color = "Black staggered"; // The health value is this. health = 100; // cute value this. lovely = 80; // age this. age = "3 years old";} // eat Panda. prototype. eat = function () {console. log ("eat bamboo");} // pull Panda. prototype. shit = function () {console. log ("pulled a glimpse of Xiang");} // sleep Panda. prototype. sleep = func Tion () {console. log ("sleeping all day");} // mate Panda. prototype. mating = function () {console. log ("pandatv's love is also for the Next Generation");} // Panda_A () {// hygiene degree this. lovehealth = 60; Extend (this, new Panda ();} // bath Panda_A.prototype.Bath = function () {console. log ("in order not to get sick, you have to speak hygiene, I went to take a bath"); // take a bath, speak hygiene + 1 this. lovehealth + = 1;} // Panda_ B () {// charm value this. charm = 90; Extend (this, new Panda ();} // pick up Panda_ B .prototype.GetGir Ls = function () {console. log ("for the prosperity of the Community, I went to pick up a girl first. Who told me that my charm is so high? "); // Bubble a girl, charm value-1 this. charm-= 1 ;}
Let's take a look at the example below. Is it the same as described above?
The attributes and behavior functions of the parent class (common) can be normally used by both the instance of the parent class and the instance of the subclass. Let's take a look at what is unique next?
The behavior that everyone is most concerned about has not been tested yet, that is, pandatv B can pick up girls and no longer adjust their tastes. Immediately Test
Through the above example, we already know what polymorphism is, and now some people have asked: We can already implement polymorphism, but we only see some more special attributes and behaviors, can we change some original attributes and behaviors? To simulate objects, we need to make the same simulation as possible, just like if there is a pandatv Class C, they have evolved more advanced and don't need to eat bamboo anymore, the scholar started to eat:
// Panda_C () {Extend (this, new Panda ();} // eat Panda_C.prototype.Eat = function () {// meal console. log ("we have to evolve more advanced than humans, so we started to eat, not eat bamboo !! ");}
As long as the attributes or behaviors are different from those of the parent class, we can override them to meet the special requirements of subclass metamorphosis.
To sum up, today we will add the ideas inherited above, so as to realize the simulation of polymorphism and OOP, and we will come to an end first, I believe that everyone has their own understanding of OOP, that is, to simulate the code into an object for writing, there are two advantages:
1. Improve the code reuse rate and improve the work efficiency.
2. The Code has been simulated on the object, so organized code facilitates management, and later maintenance and expansion.
The above is all the content of this article, hoping to help you learn.
Articles you may be interested in:
- Basic polymorphism of javascript Object-Oriented Programming
- Inheritance and Polymorphism of javascript object-oriented new rational training
- Implementation of polymorphism in js adopts a method similar to inheritance
- Learning JavaScript design patterns (polymorphism)
- Javascript is based on three main features of objects (encapsulation, inheritance, and polymorphism)