1. Reflection && Class
Suppose you have a test class defined as follows:
Class Test{public void TestFunction () {System.out.println ("here ...");}
At this point, we need an instantiated object of type test, which is usually done by:
Test test1 = new test ();
This is usually sufficient, but in some specific scenarios, new is probably not the ideal way to instantiate objects (see later)
The reflection mechanism can often compensate for the shortcomings of new in these scenarios.
The realization of reflection is inseparable from an important class: class.
The class can be simply understood as: "Class Class", as to what the specific class, which requires the program at run time derivation, so the class class generic use? Represents an unknown class.
Class<?>
Here are three ways to get the Class<?> type object for the specified class:
Mode 1: Obtained through the GetClass () method of the object class (usually rarely in this way) test test_1 = new test (); class<?> cls_1 = Test_1.getclass (); System.out.println (Cls_1.getname ());
Method 2: Use "class. Class" To obtain class<?> cls_2 = Test.class; System.out.println (Cls_2.getname ());
Mode 3: Use the Forname method in Class (common mode) class<?> Cls_3 = Class.forName ("Com.test.mine.Test"); System.out.println (Cls_3.getname ());
With this Class<?> object, you can call its internal newinstance () method to get a request object.
Next, instantiate the target object:
Test obj = (test) cls_3.newinstance (); The return value type of this function is Object
2. Application Scenario
There is a scenario where, for example, in Factory mode, we may need to constantly delete and remove factory-produced products, in such a scenario, if you use new to implement the factory production method, because new must invoke the characteristics of the specified class construction method, the overall structure of the program is very rigid.
package com.test.mine;/////traditional Interface fruit {public void eat () ;} Class apple implements fruit {public void eat () {System.out.println ("Eat apples.") ");}} Class orange implements fruit {public void eat () {System.out.println ("Eat oranges.") ");}} Public class classfactory {public static fruit getinstance ( String classname) {if ("Apple". Equals (ClassName)) {return new apple () ;} if ("Orange". Equals (ClassName)) {Return new orange ();} ..... return null;} Public static void main (String[] args) {Fruit apple = Classfactory.getinstance ("Apple"); Apple.eat (); Fruit orange = classfactory.getinstance ("Orange"); Orange.eat ();}}
For example: Once a product has changed, the traditional factory model that violates the "low coupling" feature requires manual modification of the factory's production methods, and if the production method of the plant can be used to dynamically deduce what products are produced and produce the corresponding products, then the coupling of the system will be improved a lot.
The specific implementation is as follows:
package com.test.mine;interface newfruit {public Void eat () ;} Class newapple implements newfruit {public void eat () {System.out.println (" Eat apples. ");}} Class neworange implements newfruit {public void eat () {System.out.println ( "Eat oranges." ");}} Public class newfactory {public static newfruit getinstance (String ClassName) {newfruit ret = null;try {ret = (newfruit) class.forname (className). Newinstance ();} catch (Exception e) {// todo: handle exception}return ret;} Public static void main (String[] args) {NewFruit apple = Newfactory.getinstance ("Com.test.mine.NewApple"); Apple.eat (); Newfruit orange = newfactory.getinstance ("Com.test.mine.NewOrange"); Orange.eat ();}}
For the factory model, an article will be written to introduce
3. Reflection method
Reflection method Class<?> Cls_4 = Class.forName ("Com.test.mine.Test"); Method m1 = Cls_4.getdeclaredmethod ("TestFunction"); Object obj = Cls_4.newinstance (); M1.invoke (obj);
The reflection mechanism in Java