Java's rtti mechanism syntax

Source: Internet
Author: User
Tags constructor

Java uses a class object to implement its own RTTI capabilities-even if all we have to do is work like styling. The class class also provides a number of other ways to facilitate our use of rtti. The
must first obtain a handle to the appropriate class object. As shown in the previous example, one approach is to use a string and a Class.forName () method. This is very handy because you do not need an object of that type to get the class handle. However, for a type that is of interest to you, if you already have one of its objects, you can invoke a method that is part of the object root class in order to get the class handle: GetClass (). Its function is to return a specific class handle that represents the actual type of the object. class provides several interesting and useful methods, as you can see from the following example:
 

: Toytest.java//Testing Class class interface Hasbatteries {} interface Waterproof {} interface shootsthings {} clas s Toy {//Comment out of the following default//constructor to check//Nosuchmethoderror from (*1*) Toy () {} to Y (int i) {}} class Fancytoy extends Toy implements Hasbatteries, waterproof, shootsthings {Fancytoy () { Super (1);
    } public class Toytest {public static void main (string[] args) {class c = null;
    try {c = class.forname ("Fancytoy");
    catch (ClassNotFoundException e) {} printinfo (c);
    Class[] faces = C.getinterfaces ();
    for (int i = 0; i < faces.length i++) Printinfo (faces[i));
    Class cy = C.getsuperclass ();
    Object o = null; 
      try {//Requires default constructor:o = Cy.newinstance ();//(*1*)} catch (Instantiationexception e) {}
  catch (Illegalaccessexception e) {} printinfo (O.getclass ());
The static void Printinfo (Class cc) {System.out.println (      "Class Name:" + cc.getname () + is interface?
  ["+ cc.isinterface () +"]); }
} ///:~

The

shows that class Fancytoy is quite complex because it inherits from toy and implements Hasbatteries,waterproof and Shootsthings interfaces. A class handle is created in main () and initialized to Fancytoy with the forname () in the corresponding try block. The
Class.getinterfaces method returns an array of class objects that represent the interfaces contained within the class object.
If you have a class object, you can also use Getsuperclass () to query what the direct base class of the object is. This, of course, returns a class handle that can be used for further queries. This means that there is a complete opportunity to investigate the full hierarchy of objects at runtime.
on the surface, the newinstance () method of class seems to be another means of cloning (clone ()) an object. But there is a difference between the two. With Newinstance (), we can create a new object without ready-made objects for cloning. As the previous program demonstrates, there was no toy object, only a handle to the class object of cy--, Y. Use it to implement the virtual builder. In other words, we express: "Although I do not know what your exact type is, but please do your right to create yourself anyway." In the example above, CY is just a class handle, and no further type information is known during compilation. Once you have created an instance, you can get an object handle. But that handle points to a toy object. Of course, if you want to send any other message that can be received except object, you must first do some research and then sculpt. In addition, a class created with newinstance () must have a default builder. There is no way to create an object with a Non-default builder with newinstance (), so there may be some limitations in Java 1.0. However, the "reflection" API of Java 1.1 (discussed in the next section) allows us to dynamically use any builder in the class. The last method in the
program is Printinfo (), which obtains a class handle, obtains its name through GetName (), and Interface () to investigate whether it is an interface. The output of the
program is as follows:
 

Class Name:fancytoy is interface? [FALSE]
Class Name:hasbatteries is interface? [True]
Class Name:waterproof is interface? [True]
Class Name:shootsthings is interface? [True]
Class Name:toy is interface? [FALSE]

So using class objects, we can almost investigate the bastard of an 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.