JavaScript inheritance (next) why Inherit

Source: Internet
Author: User
Tags extend inheritance

Quiz1

Does JavaScript really need classes (class)?

Let's first look at some of the features of other object-oriented languages (class), such as Java.

Parent class and Child class

The parent class (superclass) and subclasses (subclass) are not meant to solve the problem of father and son, but to solve the inclusion relationship of the class, we use sub to represent "subclass" and "parent class" with SUP, then there are:

Sub Sup

There is a difference, for example, that we can usually use subclasses as parents, but we don't think of our sons as fathers.

Or it can be said that the parent class and subclass are not meant to resolve the same method or attribute between classes.

As an example

Some people like to do this:

We need some animal classes to create some moving animals on the screen, but some of the moving animals are flying in the air and some are walking on the road.

So create two parent classes, one is fly, one is walk:

Class fly{
    Fly () {}
}
    
class walk{
    Walk () {}
}

Then the Lions (and other animals on the road) belong to the Walk category, and eagles (and other animals that fly in the sky) belong to the fly Category:

Class Lion extend walk{
}
    
class Eagle extend fly{
}

Finally, create some instances of the lion and Eagle classes and invoke the appropriate method, and there will be some lions and eagles moving on the screen.

But this may not be a good design, for example, tomorrow the boss suddenly a pat on the brain, he wants to have a kind of animal called Tianma (Pegasus), they will fly in the sky, and will walk on the road, sometimes to fly, time to walk.

In this case, the scheme is completely useless.

Why did the design fail?

Inheritance is conditional, and subclasses must be able to transition strictly upward (into a parent class).

In the example above:

Lions (Lion) are assumed to be equated with walking animals (Walk), and Eagles (Eagle) are assumed to be equated with flying animals (Fly).

This seems to be a success, because the subclass can be strictly upward transition, but he has hidden trouble.

When a kind of Tianma (Pegasus) is involved in the inside, we found that the lion is actually just "walking animals", the eagle is actually only "flying animals", this does not mean that animals can only fly or walk, so that will fly and walk the Tianma can not find their own ownership.

This example is a good proof that subclasses and parent classes are not designed to solve the same method between classes:

Some animals walk and need to have a walking (Walk) method, but this should not be implemented by subclasses and parent classes.

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.