Interchange use of multi-form objects

Source: Internet
Author: User

Typically, inheritance ends up with a series of classes, all of which are based on a unified interface. We use an inverted tree to illustrate this point (note ⑤):

⑤: "The uniform notation Method" is used here, and this is the main method used in this book.

One of the important things we want to do with such a series of classes is to treat the object of the derived class as an object of the underlying class. This is important because it means that we simply write a single code that ignores the specific details of the type and deals only with the underlying class. That way, the code can be separated from the type information. So it's easier to write and easier to understand. In addition, if you add a new type by inheritance, such as "triangles," the code we write for the new type of geometry will work as well as in the old type. So the program has a "scalability", with "scalability."
Based on the above example, let's say that we write a function in Java:

void Dostuff (Shape s) {
  s.erase ();
  // ...
  S.draw ();
}


This function can communicate with any geometry (shape), so it is completely independent of any particular type of object it is to depict (draw) and delete (erase). If we use the Dostuff () function in some other programs:

Circle C = new Circle ();
Triangle T = new triangle ();
Line L = new Line ();
Dostuff (c);
Dostuff (t);
Dostuff (l);


Then the call to Dostuff () will work automatically, regardless of the object's specific type.
This is actually a very useful programming skill. Consider the following line of code:
Dostuff (c);
At this point, a circle (round) handle is passed to a function that expects the shape (shape) handle. Because a circle is a geometrical shape, dostuff () can be handled correctly. In other words, all dostuff () can send a shape message, circle can also receive. So it is safe to do so without causing errors.
The process of dealing with a derived type as its basic type is called "upcasting" (tracing the shape). where "cast" is created according to a ready-made model, and "up" indicates that the direction of inheritance is from "above"-that is, the base class is at the top, and the derived class expands below. Therefore, modeling according to the basic class is a process that inherits from above, namely "upcasting".
In object-oriented programs, it is often used to trace the modeling technology. This is a good way to avoid investigating the exact type. Take a look at the code in Dostuff ():

S.erase ();
// ...
S.draw ();

Note that it does not say this: "If you are a circle, do it; If you are a square, do it; Wait". If you write code like that, you need to check all possible types of shape, such as circles, rectangles, and so on. This is obviously very troublesome, and each time you add a new shape type, you have to modify it accordingly. Here, we just say: "You are a geometrical shape, I know you can delete yourself, namely erase (), please take that action yourself, and control all the details yourself." ”

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.