The runtime type recognition Rtti mechanism of Java Advanced Tutorials _java

Source: Internet
Author: User
Tags class definition

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

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

Class classes

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

Each class object represents one of the other classes. For example, in the following program, the class object C1 represents the human classes, and the C2 represents the woman class.

Copy Code code as follows:

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 a Women object to a reference to a human object, the class object that the object points to is still woman.

Every object in Java has a corresponding class class object, so we can always know the class that an object "really" belongs to by using the class object. Regardless of the type conversions we make to the reference, the object itself corresponds to the same class object. When we call a method through 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 to invoke the class object.

Copy Code code as follows:

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 ());
}
}

The above shows two ways:

The 1.forName () method receives a string as an argument, which is the name of the class. This returns the corresponding Class class object.

The 2.woman.class method is a class member that invokes the classes directly. This returns the corresponding Class class object.

Methods of class classes

Class objects record information about the corresponding classes, such as the name of the class, the package where the class is located, and so on. We can call the appropriate methods, such as:

Copy Code code as follows:

GetName () returns the name of the class
Getpackage () returns the package where the class resides

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

Copy Code code as follows:

Human Newperson = C1.newinstance ();

Newinstance () invokes the default construction method that does not contain parameters.

We can get the members of the class definition:

Copy Code code as follows:

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.

Class class more ways to query the official documents:

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

Class class loading

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

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

After the class object has been loaded successfully, the other human object's creation and related operations will reference the class object.

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.