Application of the reflection mechanism - java design patterns

       Java reflection mechanism means that: in the operating state, for any class, are made known to all properties and methods of this class; for any object, are able to call any of its methods and properties; this dynamic information acquired and dynamic function call the object's method is called reflection mechanism of Java language.

 

 class is a class, the class name acquired by reflection.

One,

 

import java.lang.reflect.*;
class A{
    public A(){
        System.out.println("A()");
    }
    public A(int m,String n){
        System.out.println("A(int m,String n)");
    }
    public A(int m){}
    
}
public class test {
    public static void main(String[] args) throws Exception {
        Class c = Class.forName("A");   //反射机制
        Constructor con[]=c.getConstructors (); 
          // define Constructor con array type, for holding a constructor class A 
        con [0] .newInstance ();    // corresponds to the order of the sequence constructor 
        con [1] .newInstance (12, "Hai" ); 
        
    } 

} 
 

Results are as follows

 two,

   CON = c.getConstructor Constructor (int.class, String.class ); create the specified object constructor con: through different parameters, get the constructor 
   con.newInstance (200, "the Hello" ); call newInstance () function newly created objects
import java.lang.reflect.*;
class A{
    public A(){
        System.out.println("A()");
    }
    public A(int m,String n){
        System.out.println(m+":"+n);
    }
    public A(int m){}
    
}
public class test {
    public static void main(String[] args) throws Exception {
        Class c = Class.forName("A");   //反射机制
        Constructor con=c.getConstructor(int. Class ., String class ); 
          // developed by the constructor parameter points 
        con.newInstance (200 is, "Hello" );   
        
    } 

}

 

The results are as follows:

 

 

 

Third, the specific application

 1 import java.lang.reflect.*;
 2 import java.util.Scanner;
 3 interface IShape{
 4     void input();
 5     double getArea();
 6 }
 7 class Circle implements IShape{
 8     double r;
 9     public Circle(){}
10     public void input(){
11         System.out.println("please input r:");
12         Scanner sc = new Scanner(System.in);
13         r = sc.nextDouble();
14     }
15     public double getArea(){
16         return 3.14*r*r;
17     }
18     
19 }
20 class Rect implements IShape{
21     double h,w;
22     public Rect(){}
23     public double getArea(){
24         return h*w;
25     }
26     public void input(){
27         System.out.println("please input h,w:");
 28          Scanner SC = new new Scanner (the System.in);
 29          H = sc.nextDouble ();
 30          W = sc.nextDouble ();
 31 is          
32      }
 33 is  }
 34 is  
35  public  class Test {
 36      public  static  void main (String [] args) throws Exception {
 37 [      / * Circle Circle new new C = (); // write is no problem, but there is no application reflection
 38 is          c.input ();
 39          Double c.getArea Area = ();
 40          System.out.println ( "area =" + area
 );41 is      * /     
42 is          
43 is          / *     Class C = the Class.forName ( "Circle"); // write no problem, but also the application of reflection, but this method is not optimal
 44           // If we wish to calculate the area Rect, it Circle all have to change all the Rect, trouble, so the application interface
 45              Circle obj = (Circle) c.newInstance ();
 46 is              obj.input ();
 47              Double obj.getArea Area = ();
 48              the System.out. the println ( "Area =" + Area);
 49          * / 
50              Class C = the Class.forName ( "Circle");    // this just enough to change the name 
51 is              the IShape obj = (the IShape) c.newInstance ();
 52 is              obj.input ();
 53 is              Double area = obj.getArea();
54             System.out.println("area="+area);
55         
56     
57         
58     }
59 
60 }

 

Guess you like

Origin www.cnblogs.com/ttting/p/11479139.html