Chapter 14. Type information (1)

Source: Internet
Author: User

Run-time type information allows you to discover and use type information while the program is running.

Java recognizes objects and classes at runtime in two ways: (1) "traditional" RTTI, which assumes that we already know all types at compile time, and (2) "reflection" mechanism, which allows us to discover and use information about classes at runtime.

First, why need Rtti (run-time-type-information)

The basic purpose of object-oriented programming is to have the code manipulate only the reference of the base class, so that if you want to add a new class under the base class, the original code will not be affected.

For example:

The animal in the diagram is the base class, at the top, and the derived class expands downward.

Look first, what happens if we don't manipulate the base class?

If we write code that does not manipulate the animal base class, but the specific animal derived classes under the animal, such as: giraffes, etc., it is bound to cause our code to change over and over again. Because giraffes run by no means the leopard run, when you want to let the leopard run, you have to change the running of the giraffe, so that our object-oriented programming purposes are not reached.

So what can be considered object-oriented programming?

We only need to manipulate the dynamic bound running behavior of animals, so that in specific animals to cover the specific run behavior can be. When this is really running, the actual running behavior in the derived class is actually run. I call this trick " cheat ".

Examples of the code are:

Chapter 14. Type information (1)

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.