Java Rtti Learning Summary

Source: Internet
Author: User
Tags class definition

Using RTTI, type information can be investigated based on an anonymous base class handle. But it is for this reason that beginners are apt to misuse it, because sometimes the method of polymorphism is sufficient. For those who used to programmed programmatically, it is easy to organize their programs into a series of switch statements. They may use Rtti to do this, thereby losing the important value of the technology in code development and maintenance. The Java requirement is that we use rtti as much as possible, only in very special cases.

But in order to take advantage of the polymorphism, we are required to have control over the underlying class definition, because sometimes within the scope of the program, we may find that the underlying class does not include the method we want. If the underlying class comes from a library or something else, rtti is a good solution: You can inherit a new type, and then add your own additional methods. Elsewhere in the code, you can detect your own specific type and invoke that particular method. This does not break the polymorphism and the extension of the program, because the addition of the new type does not require a switch statement to be found in the program. But when you add new code to a body that requires a new feature, you must use RTTI to detect your own particular type.

In the interest of a particular class, adding an attribute to the underlying class may mean that all other classes derived from that underlying class must have some meaningless "ribs". This makes the interface more ambiguous. If someone inherits from that base class and must overwrite the abstract method, this phenomenon can cause them to be troubled. For example, a class structure is now used to represent an instrument (instrument). One way to do this is to place a clearspitvalve () method in the base class instrument, assuming that we want to clean the spit Valve of all the appropriate instruments in the orchestra. But doing so would create a misconception because it implies that percussion instruments and electronic musical instruments also have sound plugs. In this case, RTTI provides a more reasonable solution to place the method in a particular class (at this point, wind, or "vent")--which is feasible. But in fact a more plausible scenario is to place Prepareinstrument () in the underlying class. Beginners often do not see this in the beginning, will generally assume that they must use RTTI.

Finally, Rtti can sometimes solve the problem of efficiency. If the code uses a lot of polymorphism, but one of the objects in the execution efficiency is very problematic, you can use Rtti to find that type, and then write a piece of appropriate code to improve its efficiency.

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.