Java foundation Rtti Runtime type recognition

Source: Internet
Author: User

Run-time type recognition (RTTI, Run-time type identification) is a very useful mechanism in Java that RTTI maintain information about a class while it is running.

Polymorphism (polymorphism) is implemented based on Rtti. The functions of Rtti are mainly implemented by class classes.

Class

The class class is "Classes of classes" (Class of classes). If a class is an abstraction and a collection of objects, then class is an abstraction and a collection of classes.

The object for each class class represents an additional class. For example, in the following program, the class object C1 represents the human class, and C2 represents the woman class.

public class test{public    static void Main (string[] args)    {        Human Aperson = new Human ();        Class C1      = Aperson.getclass ();        System.out.println (C1.getname ());
        Human Anotherperson = new Woman ();        Class C2      = Anotherperson.getclass ();        System.out.println (C2.getname ());  
}}class human{
/** * Accessor * /public int getheight () { return this.height; } /** * mutator * /public void growheight (int h) { this.height = this.height + h; } private int height; }class Woman extends human{ /** * New Method * /Public Human Givebirth () { System.out.println ("Give birth"); Return (new Human ());} }

When we call the object's GetClass () method, we get a reference to the corresponding class object.

In C2, even if we convert the reference of the women object up to a reference to the human object, the object is pointing to a class object that is still woman.

Every object in Java has a corresponding class class object, so we can always know what class the object "really" belongs to by the class object. No matter what type conversion we make to a reference, the object itself corresponds to the same class object. When we invoke a method from a reference, Java always finds the method defined in the correct class class and executes the code in that class. Because of the existence of class objects, Java is not lost because of the type's upward conversion. This is the principle of polymorphism.

GetClass: Who am I?

In addition to the GetClass () method, we have other ways of invoking the class object.

public class test{public    static void Main (string[] args)    {        class C3      = Class.forName ("Human");        System.out.println (C1.getname ());        Class C4      = Woman.class        System.out.println (C2.getname ());}      }

There are two ways of showing this:

    • The forname () method receives a string as a parameter, which is the name of the class. This returns the corresponding Class class object.
    • The Woman.class method is to call the class members of the classes directly. This returns the corresponding Class class object.

Methods for class classes

The class object records information about the corresponding classes, such as the name of the class, the package in which the class resides, and so on. We can call the appropriate method, for example:

GetName () returns the name of the class

Getpackage () returns the package that contains the class

You can use the Newinstance () method of the Class object to create objects of the corresponding class, such as:

Human Newperson = C1.newinstance ();

Newinstance () invokes the default build method with no parameters.

We can get the members of the class definition:

GetFields () returns all public data members

GetMethods () returns all public methods

You can further use the reflection analysis class. This is no longer in depth.

The class class has more ways to query official documents:

Http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Class.html

Loading of class classes

When Java creates an object of a class, such as a human class object, Java checks if there is a corresponding class object in memory.

If there is no corresponding class object in memory, Java looks for the definition of the human class in the. class file and loads the class object of the human classes.

After the class object is loaded successfully, other human object creation and related operations will refer to the class object.

Java foundation Rtti Runtime type recognition

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.