Java-Preliminary Understanding-tenth-polymorphism-benefits

Source: Internet
Author: User

One.

For example, the benefits of polymorphism can be explained by examples.

The example in this is the embodiment of a very standard succession system program. The void Eat () class in the animal class refers to: animals have the function of eating, specifically eat what, do not know.

Then create the object and run the program, which is no problem.

is to create a cat object and invoke the eating action. If there is more than one cat, there are a lot of cats, cats have to eat things (in the second, missing c1.eat c2.eat c3.eat ...). This is the time to improve the reusability of the code (to improve the reuse of this action, rather than to create the reuse of the cat, create objects This action must not be less.) ), create a function that says the cat eats, and the last one shows what it looks like when it's improved.

In this step of improving reusability, the process of eating is slightly more complicated. The original cat's object directly called the cat's eating function, now through the third method () to complete the eating of this action.

At this time the dog was created, the dog wanted to eat, and the dog used the function method (). This is not true because the parameter list of method () methods is for the Cat object.

At this time, the need to build a dog to eat something function

then the species of animals increases, increasing the number of pigs. Then the main function has to be changed to create the Pig object, as well as the pig eat food function.

So far, the program is very cumbersome to write, we have to find a way to solve such problems.

Comb your mind: Java solves problems by object-oriented. The kitten eats and directs the object to do things. The dog eats, the dog has the function of eating, I let the dog go to work, how to eat me regardless, eat what I also don't care. The pig eats, the home raises the animal to be more. At this point we need to rethink the problem, we encapsulate the content into objects and then direct the object to do things. This has simplified the problem, but the object has become more complicated. The next thing to think about is, can you direct more objects to do things? This began to look for the general character of the object, looking for common discoveries, whether cats, dogs, pigs, they are animals. So, change the way of command, "animals eat," they are all eating. In this way, the complex things are again simplified.

Just command the common types of these objects to do things, because they all belong to the generic type.

The way to recreate this meal is to target the animals. No matter which animal in the main function calls the method (), the compile run is fine.

The successes above illustrate a point, as shown in, a constantly pointing to the objects of cats, dogs, and pigs of these subclasses.

This is a simple polymorphic, dog form, animal a form (a also represents an object).

In the future, no matter what species of animals are added, the method () can be called directly, which improves reusability and increases extensibility. Later maintenance is also very convenient, unlike before, to describe the addition of a lot of code, now only need a method ().

Java-Preliminary Understanding-tenth-polymorphism-benefits

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.