JavaScript great God cultivation (7)--oop thought (polymorphic)

Source: Internet
Author: User

Hello readers, today we will proceed to the previous content, we have already talked about the inheritance, today we say that OOP is the last embodiment, that is polymorphic, because JavaScript language flexibility, so we have no way to use the interface, So this also brought a certain confusion to JS program, we do not have to worry too much about this problem, because these to the back of the ECMAScript later version will give us to solve these problems, and pull away, or back to the point, the multi-state of OOP, before we can clearly understand the inheritance is what it looks like, is to declare a parent class, and then we can write a lot of subclasses to inherit the properties and methods of the parent class, and we can implement the same function as the parent class with the least amount of code, which is inheritance. Immediately a classmate questioned: the previous inheritance I looked for a long time, but also can have an understanding, but the Lord is not aware of what is the use, why we have to write so many inheritance classes? This question, immediately to the point, ask quite the key, if there is no polymorphic existence, we said the inheritance is really useless, because all of our inheritance classes are identical copy, no characteristics. As an example:

We can clearly see the problem, Panda A, Panda B and the parent panda is exactly the same, although, we can easily write a lot of sub-class, but it is not useful, we directly use the parent class properties and methods, we can achieve our goal, so to speak, the sense that the inheritance of OOP is no use, We learned in front of the knowledge, will not waste it, haha, do not be afraid, as long as just what you have learned, there is no white learning, the following we will say the inheritance of the supplement, polymorphic, look at the example diagram:

Whether human, or other animals, continuous multiplication, continuous evolution, each descendant seems to be the same, in fact, not the same, more or less have their own unique properties or behavior, now panda A in a special case to learn to bathe, it has its own health properties, and Panda B is a master, learned to pick up the girls (uncle, I have not learned, it learned), 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 with the parent class and must have its own unique behavior or properties, So we omit the code for the same behavior or attribute (the benefits of OOP are once again reflected), let's look at the example code:


//Inherited inheritance functions
function Extend (children,parent) {
    for (var p in Parent) {
         if (typeof children[p] = = "undefined") {
             children[p] = parent[p];
       }
   }
}

//Panda parent
Function Panda () {
   //color
    This.color = "Black interleaved";
   //Health value
    this.health = +;
   //Cute value
    this.lovely =;
   //Age
    this.age = "3 years old";
}
//eat
Panda.prototype.Eat = function () {
    console.log ("Eat Bamboo");
}
//Pull
Panda.prototype.Shit = function () {
    console.log ("pull a lump of Xiang");
}
//sleep
Panda.prototype.Sleep = function () {
    console.log ("To sleep all Day");
}
//mating
Panda.prototype.Mating = function () {
    console.log ("Panda love is for the next Generation");
}

Panda A
function Panda_a () {
Wash 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 wash, I went to bathe");
One bath, wash level + 1
This.lovehealth + = 1;
}

Panda b
function Panda_b () {
Charm value
This.charm = 90;

Extend (This,new Panda ());
}
Girls
Panda_B.prototype.GetGirls = function () {
Console.log ("For the sake of the prosperity of the tribe, I went to the girls first, who called my charm so high?") ");
Bubble once, glamour value-1
This.charm-= 1;
Let's take a look at the example, is it the same as described earlier?

Whether it is an instance of a parent class, or an instance of a subclass, you can use the parent (common) attribute and the behavior function normally, and then look at the unique one.

Again to everyone's most concerned about the behavior has not been tested, that is, Panda B can pick up girls, no longer adjust everyone's taste, immediately test

Can go through the above example, we already know how the polymorphism is the same thing, and now there are classmates asked: the front we can achieve polymorphism, just see a number of unique properties and behavior, then can change some of the original properties and behavior? Because we want to simulate the object, we have to do as much as possible, like, if there is a panda class C, they evolved more advanced, no longer eat bamboo, incredibly scholar began to eat:


Panda C
function Panda_c () {

Extend (This,new Panda ());
}
Eat
Panda_C.prototype.Eat = function () {
Eat
Console.log ("We're going to evolve more advanced than humans, so we start eating and no more bamboo!! ");
}

See, as long as the parent class is not the same property or behavior, we can override the way to overwrite, so as to achieve the special requirements of sub-class metamorphosis.

Summing up, today we will add the idea of the previous inheritance, so as to achieve a multi-state simulation, OOP thought, we also first to the conclusion, I believe that we have their own understanding of OOP, is the code to imitate the object to write, so there are two advantages: 1, the code reuse rate increased, improve the efficiency. 2, the code has been simulated in the object, so that organized code to facilitate our management, and later maintenance and expansion.

JavaScript great God cultivation (7)--oop thought (polymorphic)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.