反射执行方法

 

package Fanshe;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.util.ArrayList;

import utils.Conn;

public class FConn {
    public static void main(String[] args) throws Exception {

        FConn.fConn1(Conn.class);

    }

    public static <T> ArrayList<T> fConn(Class<T> obj) throws Exception {
     Constructor<?> cons = obj.getDeclaredConstructor();
        T newInstance = obj.newInstance();
        Method method = obj.getMethod("link", new Class[] {});
        method.invoke(newInstance, null);

        return null;
    }





public static <T> ArrayList<T> fConn1(Class<T> obj) throws Exception {
    
    T newInstance = obj.newInstance();
    Method m= obj.getDeclaredMethod("link");
    m.invoke(newInstance);
    
    
    return null;
    
}

}


一万年太久,只争朝夕!
原文地址:https://www.cnblogs.com/chaoba/p/6895571.html