1. 程式人生 > >Java反射之調用內部類

Java反射之調用內部類

pre http code 引用 IE out col sin over

1. 反射調用默認訪問權限的內部類

package com.blueStarWei.invoke;

import java.lang.reflect.Method;

import com.blueStarWei.invoke.OuterClass.InnerClass;

class OuterClass {
    public void func(){
        System.out.println("Outer Class.");
    }
    
    class InnerClass {
        private String mName;
        public
InnerClass(String mName) { this.mName = mName; } void getName(){ System.out.println("Name is : "+mName); } } } public class OuterInnerClass { public static void main(String[] args) { try { /*通過反射實例化內部類 * 強大之處在於:當內部類私有化(private class InnerClass)時,也可以調用 *
*/ Class outerClass = Class.forName("com.blueStarWei.invoke.OuterClass"); Class innerClass = Class.forName("com.blueStarWei.invoke.OuterClass$InnerClass"); Method method = innerClass.getDeclaredMethod("getName"); //TODO study it //內部類newInstance的第一個參數必須是外部類實例的引用
method.invoke(innerClass.getDeclaredConstructors()[0].newInstance(outerClass.newInstance(),"Wei"));//Name is : Wei /*常規方法實例化內部類 * 當內部類私有化時,無法通過該方式實例化內部類 * */ OuterClass outerClass1 = new OuterClass(); InnerClass innerClass1 = outerClass1.new InnerClass("David"); innerClass1.getName();//Name is : David } catch (Exception e) { e.printStackTrace(); } } }

2. 反射調用靜態內部類

package com.blueStarWei.invoke;

import java.lang.reflect.Method;

class OuterClass {
    public void func(){
        System.out.println("Outer Class.");
    }
    
    static class InnerClass {
        private String mName;
        public InnerClass(String mName) {
            this.mName = mName;
        }
        void getName(){
            System.out.println("Name is : "+mName);
        }
    }
}


public class StaticInnerClass {

    public static void main(String[] args) {
        try {
            /*通過反射實例化內部類 */
            Class outerClass = Class.forName("com.blueStarWei.invoke.OuterClass");
            Class innerClass = Class.forName("com.blueStarWei.invoke.OuterClass$InnerClass");
            Method method = innerClass.getDeclaredMethod("getName");
            //靜態內部類沒有持有外部類的引用,不需要傳遞外部類實例對象的引用
            method.invoke(innerClass.getDeclaredConstructors()[0].newInstance("Wei"));//Name is : Wei
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

3. 反射調用匿名內部類

package com.blueStarWei.invoke;

class OuterClass {
    public Runnable runnable = new Runnable() {
        @Override
        public void run() {
            System.out.println("This is InnerClass.");
        }
    };
}

public class AnonymousInnerClass {
    public static void main(String[] args) {
        try {
            Class outerClass = Class.forName("com.blueStarWei.invoke.OuterClass");
            //將匿名內部類當作外部類的成員變量
            Runnable runnable = (Runnable) outerClass.getDeclaredField("runnable").get(outerClass.newInstance());
            runnable.run();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

   更多內容,請訪問:http://www.cnblogs.com/BlueStarWei/

Java反射之調用內部類