Class 定义从字符串变量派生的类的对象

Class 定义从字符串变量派生的类的对象,class,variables,object,declaration,Class,Variables,Object,Declaration,以下是用例: import java.lang.reflect.*; import java.util.logging.*; public class Invoker { public static void main(String[] args){ try { String str ="Batch_Status"; Class t = Class.forName(str); t.getMethods()[

以下是用例:

import java.lang.reflect.*;

import java.util.logging.*;
public class Invoker {
    public static void main(String[] args){
    try {
            String str ="Batch_Status";
            Class t = Class.forName(str);
            t.getMethods()[0].invoke(t,str);
        } catch (ClassNotFoundException | IllegalAccessException | InvocationTargetException ex) {
            Logger.getLogger(Invoker.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
调用器类(使用main方法)

import java.lang.reflect.*;

import java.util.logging.*;
public class Invoker {
    public static void main(String[] args){
    try {
            String str ="Batch_Status";
            Class t = Class.forName(str);
            t.getMethods()[0].invoke(t,str);
        } catch (ClassNotFoundException | IllegalAccessException | InvocationTargetException ex) {
            Logger.getLogger(Invoker.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
要调用的类(使用与类名相同的方法名,例如,在本例中为Batch_Status)

import java.lang.reflect.*;

import java.util.logging.*;
public class Invoker {
    public static void main(String[] args){
    try {
            String str ="Batch_Status";
            Class t = Class.forName(str);
            t.getMethods()[0].invoke(t,str);
        } catch (ClassNotFoundException | IllegalAccessException | InvocationTargetException ex) {
            Logger.getLogger(Invoker.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
现在的问题是,我无法使用字符串class_file的值定义任何对象,例如Invoker类中的test,比如class_file test=newclass_file()

import java.lang.reflect.*;

import java.util.logging.*;
public class Invoker {
    public static void main(String[] args){
    try {
            String str ="Batch_Status";
            Class t = Class.forName(str);
            t.getMethods()[0].invoke(t,str);
        } catch (ClassNotFoundException | IllegalAccessException | InvocationTargetException ex) {
            Logger.getLogger(Invoker.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
上面只是一个片段,在我的生产代码中,字符串变量中的值会有所不同,对于每个值,都有一个不同的类文件(类文件的名称将与字符串变量的值的名称相同)

import java.lang.reflect.*;

import java.util.logging.*;
public class Invoker {
    public static void main(String[] args){
    try {
            String str ="Batch_Status";
            Class t = Class.forName(str);
            t.getMethods()[0].invoke(t,str);
        } catch (ClassNotFoundException | IllegalAccessException | InvocationTargetException ex) {
            Logger.getLogger(Invoker.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
请建议

import java.lang.reflect.*;

import java.util.logging.*;
public class Invoker {
    public static void main(String[] args){
    try {
            String str ="Batch_Status";
            Class t = Class.forName(str);
            t.getMethods()[0].invoke(t,str);
        } catch (ClassNotFoundException | IllegalAccessException | InvocationTargetException ex) {
            Logger.getLogger(Invoker.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

这段代码演示了如何在给定字符串的情况下检索类实例:

String classString = "com.rbs.testing.Test";
    try {
        Class c = Class.forName(classString);
        Test test = (Test) c.newInstance();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
import java.lang.reflect.*;

import java.util.logging.*;
public class Invoker {
    public static void main(String[] args){
    try {
            String str ="Batch_Status";
            Class t = Class.forName(str);
            t.getMethods()[0].invoke(t,str);
        } catch (ClassNotFoundException | IllegalAccessException | InvocationTargetException ex) {
            Logger.getLogger(Invoker.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
如果您还不知道要将其转换到哪个类,则可以转储

c.newInstance();
import java.lang.reflect.*;

import java.util.logging.*;
public class Invoker {
    public static void main(String[] args){
    try {
            String str ="Batch_Status";
            Class t = Class.forName(str);
            t.getMethods()[0].invoke(t,str);
        } catch (ClassNotFoundException | IllegalAccessException | InvocationTargetException ex) {
            Logger.getLogger(Invoker.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
在对象类中,然后使用if-else子句,直到找到对象中包含的类类型

Object o =  c.newInstance();
if (o instanceof Test) {
} else if(o instanceof Test2) {
import java.lang.reflect.*;

import java.util.logging.*;
public class Invoker {
    public static void main(String[] args){
    try {
            String str ="Batch_Status";
            Class t = Class.forName(str);
            t.getMethods()[0].invoke(t,str);
        } catch (ClassNotFoundException | IllegalAccessException | InvocationTargetException ex) {
            Logger.getLogger(Invoker.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
我希望这有帮助。如果我误解了您的需要,很抱歉。

谢谢Michael

import java.lang.reflect.*;

import java.util.logging.*;
public class Invoker {
    public static void main(String[] args){
    try {
            String str ="Batch_Status";
            Class t = Class.forName(str);
            t.getMethods()[0].invoke(t,str);
        } catch (ClassNotFoundException | IllegalAccessException | InvocationTargetException ex) {
            Logger.getLogger(Invoker.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
事实上,在做一些头脑风暴的时候,我也做了同样的事情,而且效果很好。现在我也可以调用这个方法了,它也是从同一个字符串变量派生的。以下是我尝试的代码:

import java.lang.reflect.*;

import java.util.logging.*;
public class Invoker {
    public static void main(String[] args){
    try {
            String str ="Batch_Status";
            Class t = Class.forName(str);
            t.getMethods()[0].invoke(t,str);
        } catch (ClassNotFoundException | IllegalAccessException | InvocationTargetException ex) {
            Logger.getLogger(Invoker.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
当你查看你的回复时,它似乎非常相似。谢谢,我真的很感激

import java.lang.reflect.*;

import java.util.logging.*;
public class Invoker {
    public static void main(String[] args){
    try {
            String str ="Batch_Status";
            Class t = Class.forName(str);
            t.getMethods()[0].invoke(t,str);
        } catch (ClassNotFoundException | IllegalAccessException | InvocationTargetException ex) {
            Logger.getLogger(Invoker.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

问候

可能重复:很高兴我能帮助@VishalGarg!如果不是太多,请接受答案,谢谢:)
import java.lang.reflect.*;

import java.util.logging.*;
public class Invoker {
    public static void main(String[] args){
    try {
            String str ="Batch_Status";
            Class t = Class.forName(str);
            t.getMethods()[0].invoke(t,str);
        } catch (ClassNotFoundException | IllegalAccessException | InvocationTargetException ex) {
            Logger.getLogger(Invoker.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}