Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在运行时加载类并使用静态函数、枚举等(反射)?_Java_Android_Swing_Reflection - Fatal编程技术网

Java 在运行时加载类并使用静态函数、枚举等(反射)?

Java 在运行时加载类并使用静态函数、枚举等(反射)?,java,android,swing,reflection,Java,Android,Swing,Reflection,有没有人能很好地进行反思。请提出解决方案 我的任务范围如下:- 我必须在跑步时上课 使用它的静态功能 在前一个类静态函数中传递另一个运行时类(类MConfiguration)的静态枚举变量 /*具有静态枚举变量的类*/ 公共最终类MConfiguration{ public static enum Myenum { ONE("https://abc.org"), TWO("https://pqrs.org"); private String ourl; /

有没有人能很好地进行反思。请提出解决方案

我的任务范围如下:-

  • 我必须在跑步时上课
  • 使用它的静态功能
  • 在前一个类静态函数中传递另一个运行时类(类MConfiguration)的静态枚举变量
  • /*具有静态枚举变量的类*/

    公共最终类MConfiguration{

    public static enum Myenum {
    
        ONE("https://abc.org"),
    
        TWO("https://pqrs.org");
    
        private String ourl;
    
        /***
         * Constructor.
         *
         * @param url Configuration URL for this environment.
         */
        private Env(final String url) {
            ourl = url;
        }
    
    我一直坚持使用静态枚举变量。 如何通过反射来实现[timlib.init(mContext,Myenum.REFERENCE,true,true);]

    public String TIIMLIB       = "com.rog.lib.sec.timlib";
    public String TIMLIB_EVENT = "com.rog.lib.sec.timEvent";
    public String TIMIB_ENUM   = "com.rog.lib.sec.MConfiguration.Myenum";
    
    
    /**
     *  Initialise TimLib API.
     */
    public void initTimLib(Context mContext)
    {
    //  timlib.init(mContext, Myenum.REFERENCE, true, true); // actual needs to implement at runtime.
    
       Class cl = Class.forName(TIIMLIB);
    
    
    }
    

    }

    以下代码应该可以工作

    public void initTimLib(Context mContext)
    {
        //  timlib.init(mContext, Myenum.REFERENCE, true, true); // actual needs to implement at runtime.
    
           Class cl = Class.forName(TIIMLIB);
    
           Method m1 = c1.getDeclaredMethod("init", mContext.getClass(), MConfiguration.Myenum.class, boolean.class, boolean.class);
    
           m1.invoke(c1.newInstance(), mContext, MConfiguration.Myenum.ONE, true, true); // The method invocation is done on the c1 object constructed using default constructor. I assume that the default constructor or no-arg constructor is used in c1. 
    
     }
    

    如果已使用参数构造函数重写构造函数,则调用参数构造函数以获取要传递给方法m1的对象实例。让我知道这是否对你有效。

    查看此帖子:嗨,泰雅!谢谢你的回答。但我需要将枚举值作为反射(MConfiguration.Myenum.ONE)。嗨,Rana!!上面的代码应该是m1.invoke(c1.newInstance(),mContext,MConfiguration.Myenum.ONE,true,true);如果com.rog.lib.sec.timlib所需的枚举值在当前情况下为字符串(字符串url),则请使用以下方法:方法m1=c1.getDeclaredMethod(“init”,mContext.getClass(),string.class,boolean.class,boolean.class);m1.invoke(c1.newInstance(),mContext,MConfiguration.Myenum.ONE.getourl(),true,true);如果您不期望上述内容,请在此详细说明您的要求。