Java反射

一、Class類java

   1)在面向對象的世界裏,萬事萬物都是對象。(Java語言中,靜態的成員、普通數據類型不是對象)ide

package com.learn.reflect;

import java.lang.reflect.Method;

public class MethodDemo {

    public static void main(String[] args) {
        //要獲取print(int, int)方法 1.要獲取一個方法就是獲取類的信息,獲取類的信息首先要獲取類的類類型
        A a1 = new A();
        Class c = a1.getClass();
        /**
         * 2.獲取方法 名稱和參數列表來決定
         * getMethod獲取的是public的方法
         * getDelcaredMethod獲取本身聲明的方法
         */
        
        try {
            //Method m = c.getMethod("print", new Class[] {int.class, int.class});
            Method m = c.getMethod("print", int.class, int.class);
            
            //方法的反射操做
            //a1.print(10,20);方法的反射操做是用m對象進行方法的調用  效果與a1.print(10,20)一致
            //方法若是沒有返回值返回null,有返回值返回對應的返回值
            //Object o = m.invoke(a1, new Object[] {10,20});
            Object o = m.invoke(a1, 10, 20);
            System.out.println("--------------------");
            //獲取方法print(String, String)
            Method m1 = c.getMethod("print", String.class, String.class);
            //用方法進行反射操做
            //a1.print("hello", "world");
            o = m1.invoke(a1, "hello", "world");
            System.out.println("--------------------");
            //Method m2 = c.getMethod("print", new Class[] {});
            Method m2 = c.getMethod("print");
            //o = m2.invoke(a1, new Class[] {});
            o = m2.invoke(a1);
            
        } catch (Exception e) {
            e.printStackTrace();
        } 

    }

}

class A{
    
    public void print() {
        System.out.println("hello word");
    }
    
    public void print(int a, int b) {
        System.out.println(a+b);
    }
    
    public void print(String a, String b) {
        System.out.println(a.toUpperCase()+" "+b.toLowerCase());
    }
    
}
View Code

 

    類是對象,類是java.lang.Class類的實例對象,任何一個類都是Class類的實例對象。函數

二、三種獲取方式spa

    第一種獲取方式——任何一個類都有一個隱含的靜態成員變量(class)code

    Class c1 = Foo.class;對象

    第二種獲取方式——已知該類的對象,經過getClass方法獲取blog

    Class c2 = foo.getClass();繼承

    c1,c2表示了Foo類的類類型get

    第三種獲取方式io

    Class c3 = null;

    try{

          c3 = Class.forName("com.xxx.xxx.Foo");//包含包名

    }catch(ClassNotFoundException e){

      e.printStackTrace();

    }

 三、經過類類型獲取該類的實例對象

    前提-->該類必須具有無參構造函數

    Foo foo = (Foo)c1.newInstance();

 

四、應用場景——動態加載類,在線升級等

 

五、getName()獲取類名;getSimpleName()獲取不帶包名的類名;getReturnType()獲得方法返回值類型的類類型;

getParameterTypes() 獲取參數類型----》獲得參數列表的類型的類類型

  例:

package com.learn.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

//打印類的一些信息
public class ClassUtil {
    
    /**
     * 打印成員方法信息
     */
    public static void printMethodMessage(Object obj) {
        
        Class c = obj.getClass();
        //獲取類的名稱
        System.out.println("類名是:"+c.getName());
        
        /**
         * getMethods方法獲取的是全部的public的方法,包括父類繼承而來的
         * getDeclaredMethods()獲取本身聲明的全部方法
         */
        Method[] ms = c.getMethods();
        
        for(int i=0; i<ms.length; i++) {
            //獲得方法返回值類型的類類型
            Class returnType = ms[i].getReturnType();
            System.out.print(returnType.getName()+" ");
            //獲得方法的名稱
            System.out.print(ms[i].getName()+"(");
            
            // 獲取參數類型----》獲得參數列表的類型的類類型
            Class[] paramTypes = ms[i].getParameterTypes();
            
            for(Class pc:paramTypes) {
                System.out.print(pc.getName()+",");
            }
            System.out.println(")");     
            
        }
        
        
        
    }

    /**
     *打印成員變量信息 
     */
    public static void printFieldMessage(Object obj) {
        
        Class c = obj.getClass();
        /*
         * 成員變量也是對象
         * java.lang.reflect.Field
         * Field類封裝了關於成員變量的操做
         * getFields()方法獲取的是全部的public的成員變量的信息
         * getDeclaredFields獲取的是該類本身聲明的成員變量的信息
         */
        //Field[] fs = c.getFields();
        Field[] fs = c.getDeclaredFields();
        for(Field field:fs) {
            //獲得成員變量的類型的類類型
            Class fieldType = field.getType();
            String typeName = fieldType.getName();
            //獲得成員變量的名稱
            String fieldName = field.getName();
            System.out.println(typeName+" "+fieldName);
        }
    }
    
    public static void printConMessage(Object obj) {
        Class c = obj.getClass();
        /**
         * 構造函數也是對象
         * java.lang.Constructor中封裝了構造函數的信息
         * getConstructors獲取全部的public的構造函數
         * getDeclaredConstructors獲得全部的構造函數
         */
//        Constructor[] cs = c.getConstructors();
        Constructor[] cs = c.getDeclaredConstructors();
        for(Constructor con:cs) {
            System.out.print(con.getName()+"(");
            //獲取構造函數的參數列表--》獲得參數列表的類類型
            Class[] paramTypes = con.getParameterTypes();
            for(Class pc:paramTypes) {
                System.out.print(pc.getName()+",");
            }
            
            System.out.print(")");
        }
    }

}
View Code 

 

六、方法的反射 invoke      method.invoke(對象,參數列表)

package com.learn.reflect;

import java.lang.reflect.Method;

public class MethodDemo {

    public static void main(String[] args) {
        //要獲取print(int, int)方法 1.要獲取一個方法就是獲取類的信息,獲取類的信息首先要獲取類的類類型
        A a1 = new A();
        Class c = a1.getClass();
        /**
         * 2.獲取方法 名稱和參數列表來決定
         * getMethod獲取的是public的方法
         * getDelcaredMethod獲取本身聲明的方法
         */
        
        try {
            //Method m = c.getMethod("print", new Class[] {int.class, int.class});
            Method m = c.getMethod("print", int.class, int.class);
            
            //方法的反射操做
            //a1.print(10,20);方法的反射操做是用m對象進行方法的調用  效果與a1.print(10,20)一致
            //方法若是沒有返回值返回null,有返回值返回對應的返回值
            //Object o = m.invoke(a1, new Object[] {10,20});
            Object o = m.invoke(a1, 10, 20);
            System.out.println("--------------------");
            //獲取方法print(String, String)
            Method m1 = c.getMethod("print", String.class, String.class);
            //用方法進行反射操做
            //a1.print("hello", "world");
            o = m1.invoke(a1, "hello", "world");
            System.out.println("--------------------");
            //Method m2 = c.getMethod("print", new Class[] {});
            Method m2 = c.getMethod("print");
            //o = m2.invoke(a1, new Class[] {});
            o = m2.invoke(a1);
            
        } catch (Exception e) {
            e.printStackTrace();
        } 

    }

}

class A{
    
    public void print() {
        System.out.println("hello word");
    }
    
    public void print(int a, int b) {
        System.out.println(a+b);
    }
    
    public void print(String a, String b) {
        System.out.println(a.toUpperCase()+" "+b.toLowerCase());
    }
    
}
View Code
相關文章
相關標籤/搜索