Tij Reading Notes (Tenth chapter)

Source: Internet
Author: User
Tags anonymous command line constructor interface reference reflection wrapper
Notes
10: Detection Type Runtime type recognition (Run-time type identification, abbreviated as RTTI).

Why would you need rtti  collection is a tool that has only one purpose, that is to keep other objects for you. So for the sake of generality, these collection should be able to hold anything. So they hold object.     Class object     to know how Java Rtti works, you must first know how the type information is represented when the program is running. This is done by a special object that holds the class's information, called the class object. The general object of the class is actually created by the class object. Each class in the     program must have a class object. That is, each time you write and compile a new class, you create a new one (and, so to speak, the object is stored in a. class file of the same name). When the program is running, when you need to create an object of that kind, the JVM checks to see if it loads the class object. If not, the JVM will look for the. class file and load it. It is also known that Java programs are not fully loaded at boot time, which is not the same as many traditional languages.         class.forname ("Name of a class");    this is a static method of class ( Common to all Class objects). Class objects, like other objects, can also be manipulated with reference (which is what the loader does), and forname () is one way to get its reference. It wants a string that represents the name of the class as an argument (be sure to pay attention to the spelling and the capitalization!). )。 This method returns the reference of class, and there is a side effect to see if this string says that the class is loaded or not, and if not, load it immediately. If Class.forName () does not find the class it is going to load, it throws a classnotfoundexception.         class Constants     Java also provides a way to get the reference of a class object: "Class Constants" (class literal) ".       the name of the class. class;    This kind of writing is not only simpler, but also more secure, because it is checked at compile time. Furthermore, because there is no method invocation, it is more efficient to perform. The         class constants can be used not only for generic classes, but also for interfaces, arrays, and primitive types. In addition, each primitive wrapper class also has a standard, data member named type. This type can return the reference of the class object of "wrapper class" associated with this primitive, just like this:              ... Equal to ...    boolean.class    boolean.type    char.class        character.type    byte.class       byte.type    short.class      short.type    Int.class         integer.type    long.class        long.type    float.class      float.type    double.class     double.type    void.class        VOID.TYPE        I like to use ". Class" as much as possible because it's consistent with the normal class.     Prior to the conversion of the first check     so far, you see the Rtti form has:      1. Classic type conversions: such as "(Shape)", this conversion is checked by Rtti. If you do the wrong conversion, it throws a classcastexception.       2. A class object that represents an object type. You can query the class object at run time to get the information you need.         without explicit type conversions, the compiler does not allow you to assign objects to the reference of derived classes.     Java also has a third form of rtti. This is the instanceof keyword, which tells you that the object is not an instance of a class. It returns a Boolean value.     using class constants     dynamic instanceof    isinstance () can completely replace instanceof. The equality of     instanceof vs. class

The RTTI Syntax class.getinterfaces () method returns an array of Class objects.  The objects in the array represent the interfaces that it implements, respectively. If you have a class object in your hand, you can also use Getsuperclass () to ask the closest parent of the class.  Of course, this will return a class of reference, so you can then ask, when the program is running, you can find the complete relationship of the object. The Newinstance () method of class is like a method of another clone () object.  However, you can create a new object out of thin air with newinstance (). Printinfo () method, which takes the reference of a class object as the parameter, extracts the class name with GetName (), and uses Isinterface () to determine whether it is an interface. This way, you can find out all the information about the object you want to know by just the class object.

Reflection: Run-time class Information   Java provides support for component-based programming in the form of JavaBeans.   Create objects on the remote machine and run programs over the network. This becomes the "Remote method call" (invocation is the abbreviation for RMI). It allows a Java program to distribute objects to many machines.   In addition to class classes, there is also a class library, Java.lang.reflect also supports reflection. When the class library has Field,method, and the constructor class (which implements the member interface) runs, the JVM creates objects of this class to represent the members of the unknown class. You can then use constructor to create new objects, use Get () and set () to read and modify the member data associated with the field formation, and invoke () method to call methods associated with the method object. In addition, you can use GetFields (), GetMethods (), GetConstructors () to get an array of objects representing member data, methods, or constructors. Thus, even if you do not get any information at compile time, you have the means to ask all types of information about anonymous objects at run time.   It is important that reflection is not a hex. When you are dealing with an object of unknown class with reflection, the JVM (like the ordinary Rtti) first looks at the object as belonging to that particular type, but after that it has to load the class object to work. That is, both locally and from the network, the JVM has to get the. class file anyway. So the real difference between rtti and reflection is that rtti the compiler to open and check. class files at compile time. In other words, you are invoking the object in a "normal" way. For reflection, the compile time does not get the. class file, so it opens and checks the file at run time.     A procedure for extracting a class method     generally speaking, you are not likely to use reflection directly; Java has this function to support some of the suppressed features, such as the serialization of objects and JavaBeans. In some cases, however, it can be useful to dynamically extract information about a class. The GetMethods () and GetConstructors () methods of the     class return a method and a constructor number respectivelyGroup. These two classes also include a method of splitting the names, parameters, and return values of the methods they represent. But you can also do it like this, using ToString () to get a string that includes all signature signatures of this method. The rest of the code is used to extract command line information and to determine whether the method feature matches the string you enter (with indexof ()) and lists the matching methods.

Summary: Rtti allows you to use an anonymous base class reference to get the exact type of information for an object. In the case of not knowing multiple method calls, this is natural, so the novice will naturally think of it, and then use the wrong place, for many people from the process-oriented programming language, at the beginning, they are not accustomed to throw away the switch statements. So when they use Rtti to program, they miss the benefits of programming and code maintenance that comes with polymorphism.  The original meaning of Java is to allow you to use polymorphism throughout the program, knowledge in the last resort to use Rtti. But to use multiple method calls correctly, you have to be able to control the definition of the base class, because when you extend the program, you may find that the base class does not have the method you want. If the base class is from a class library or is controlled by someone else, then Rtti is a solution: You can inherit a new class and add your own method. In other parts of the program, you can detect this type and invoke those special methods. This does not break the polymorphism, nor does it affect the extensibility of the program, because adding a new type does not make you go around modifying the switch statement.  However, if you add a nag to use the new feature in the main part of the program, you must use RTTI to check the exact type of object. Rtti will also be used to solve the problem of efficiency. Let's say you write a good multiple program, but when you run it, you find an object that is very slow to reflect. So, you can use Rtti to pick up the object, and then specifically for its problems to write code to improve the efficiency of the program, but the programming time to avoid the early words code. This is a very tempting trap. It's best to let the program run and then judge if it's running fast enough. Only think it's not fast enough, you should go to solve the efficiency problem-with profiler.


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.