Java 从字符串获取类类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2408789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:12:33  来源:igfitidea点击:

Getting Class type from String

javaclassreflection

提问by Steven

I have a Stringwhich has a name of a class say "Ex"(no .classextension). I want to assign it to a Classvariable, like this:

我有一个String类的名称说"Ex"(没有.class扩展名)。我想将它分配给一个Class变量,如下所示:

Class cls = (string).class

How can i do that?

我怎样才能做到这一点?

采纳答案by Bozho

Class<?> cls = Class.forName(className);

But your classNameshould be fully-qualified - i.e. com.mycompany.MyClass

但你className应该是完全合格的 - 即com.mycompany.MyClass

回答by Hans Westerbeek

It should be:

它应该是:

Class.forName(String classname)

Class.forName(String classname)

回答by Manrico Corazzi

Not sure what you are asking, but... Class.forname, maybe?

不确定你在问什么,但是... Class.forname,也许?

回答by rsp

You can use the forNamemethod of Class:

您可以使用以下forName方法Class

Class cls = Class.forName(clsName);
Object obj = cls.newInstance();

回答by JuanZe

String clsName = "Ex";  // use fully qualified name
Class cls = Class.forName(clsName);
Object clsInstance = (Object) cls.newInstance();

Check the Java Tutorial trail on Reflection at http://java.sun.com/docs/books/tutorial/reflect/TOC.htmlfor further details.

http://java.sun.com/docs/books/tutorial/reflect/TOC.html上查看有关反射的 Java 教程路径以获取更多详细信息。

回答by gmhk

You can get the Class reference of any class during run time through the Java Reflection Concept.

您可以在运行时通过 Java 反射概念获取任何类的 Class 引用。

Check the Below Code. Explanation is given below

检查下面的代码。解释如下

Here is one example that uses returned Class to create an instance of AClass:

下面是一个使用返回的 Class 创建 AClass 实例的示例:

package com.xyzws;
class AClass {
    public AClass() {
        System.out.println("AClass's Constructor"); 
    }  
    static {   
        System.out.println("static block in AClass");  
    }
}
public class Program {   
    public static void main(String[] args) {
        try {       
            System.out.println("The first time calls forName:");   
            Class c = Class.forName("com.xyzws.AClass");      
            AClass a = (AClass)c.newInstance();    
            System.out.println("The second time calls forName:");  
            Class c1 = Class.forName("com.xyzws.AClass"); 
        } catch (ClassNotFoundException e) { 
            // ...
        } catch (InstantiationException e) {  
            // ...
        } catch (IllegalAccessException e) { 
            // ...
        }     
    }
}

The printed output is

打印的输出是

    The first time calls forName:
    static block in AClass
    AClass's Constructor
    The second time calls forName:

The class has already been loaded so there is no second "static block in AClass"

该类已经加载,因此没有第二个“AClass 中的静态块”

The Explanation is below

解释如下

Class.ForName is called to get a Class Object

调用 Class.ForName 获取类对象

By Using the Class Object we are creating the new instance of the Class.

通过使用类对象,我们正在创建类的新实例。

Any doubts about this let me know

对此有任何疑问让我知道