Java's need for Rtti

Source: Internet
Author: User

Consider the familiar class structure example below, which exploits the polymorphism. The general type is the shape class, and the specially derived types are circle,square and triangle.

This is a typical schematic diagram of the class structure, the base class is at the top, and the derived class is stretched downward. The basic goal of object-oriented programming is to use a lot of code to control the handle of the underlying type (this is shape), so if you decide to add a new class (such as rhomboid, derived from shape) to extend the program, it will not affect the original code. In this example, the dynamic binding method in the Shape interface is draw (), so what the client programmer does is call draw () through a normal shape handle. Draw () will be overwritten in all derived classes. And because it is a dynamic binding method, it behaves correctly even though it is invoked through a normal shape handle. This is the role of polymorphism.
So, we typically create a particular object (Circle,square, or triangle), trace it back to a shape (ignoring the special type of object), and then use the anonymous shape handle in the remainder of the program.
As a brief review of the polymorphism and tracing modelling, you can encode the above example as follows (if you are having difficulty executing this program, refer to Chapter 3rd 3.1.2 subsection "assignment"):

: Shapes.java
package C11;
Import java.util.*;

Interface Shape {
  void Draw ();
}

Class Circle implements Shape {public
  void Draw () {
    System.out.println ("Circle.draw ()");
  }

Class Square implements Shape {public
  void Draw () {
    System.out.println ("Square.draw ()");
  }

Class Triangle implements Shape {public
  void Draw () {
    System.out.println ("Triangle.draw ()");
  }
Public

Class The Shapes {public
  static void Main (string[] args) {
    vector s = new Vector ();
    S.addelement (New Circle ());
    S.addelement (New Square ());
    S.addelement (new triangle ());
    Enumeration E = S.elements ();
    while (E.hasmoreelements ())
      ((Shape) e.nextelement ()). Draw ();
  }
///:~


The underlying class can be encoded into a interface (interface), an abstract (abstract) class, or a generic class. Since shape has no real members (i.e., defined members) and does not care that we create a pure Shape object, the most appropriate and flexible expression is to use an interface. And because you don't have to set all those abstract keywords, the whole code looks fresher.
Each derived class overrides the underlying class draw method, so it has different behavior. Creates a particular type of shape in main () and then adds it to a vector. This is where the modelling takes place, because vector only holds the object. Because everything in Java (except the basic data type) is an object, the vector can also hold the Shape object. But in the process of tracing to object, any special information will be lost, including the fact that the object is geometric. For vectors, they are just object.
When you extract an element from a vector using nextelement (), the situation becomes slightly more complex. Because vectors only hold object, nextelement () naturally produces an object handle. But we know it's actually a shape handle and want to send the shape message to that object. So we need to form a shape with the traditional "(Shape)" style. This is the most basic form of Rtti, because in Java, all styling is checked during run time to ensure its correctness. That's what Rtti is all about: at runtime, the type of object is identified.
In this case, the RTTI is only part of the design: Object shape into shape, not as circle,square or triangle. That's because the only thing we can be sure of right now is that the vectors are filled with geometry, and they don't know the exact category. During the compilation, we are certainly based on our own rules, and during the compilation, it is through modelling to confirm this point.
The situation is now controlled by the polymorphism and the appropriate method is invoked for shape to determine whether the handle is providing circle,square or providing triangle. And in general, it is necessary to ensure the use of a multiple-form scheme. Because we want our code to know as little as possible about the specific types of objects, just focus on the general information of a certain class of objects (here is shape). Only in this way will our code be easier to implement, understand, and modify. Therefore, polymorphism is a general goal of object-oriented programming.
However, if you encounter a special programming problem, only after you know the exact type of the regular handle, the easiest way to solve the problem, this time how to do? For example, we sometimes want our users to turn a specific type of geometry (such as a triangle) into purple to highlight them and quickly find all the shapes of that type. The RTTI technique is used to query the exact type of a shape handle reference.

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.