我需要按类名创建类的实例,问题是这些类恰好是子类(我已经有了父类的实例)。我记得class.forName()无法加载子类。在C ++中,我可以使用从名称到静态函数指针的查找表来执行此操作,但这种动态子类加载甚至可以在Java中使用吗?
谢谢。
更新
感谢您的回答,但它看起来不适用于以下示例:
AbStractTest.java
public abstract class AbstractTest{
AbstractTest(int i){}
class InnerTest
{
InnerTest(){
System.out.println("inner test class");
}
void test(){
System.out.println("inner test");
}
}
}
Test.java
public class Test extends AbstractTest
{
Test(int i){
super(i);
}
public static void main(String[] args) throws Exception
{
Test t = new Test(1);
AbstractTest.InnerTest innerTest = (AbstractTest.InnerTest)Class.forName("AbstractTest$InnerTest").getConstructor(AbstractTest.class).newInstance(t);
innerTest.test();
}
}
它最终导致以下错误:
Exception in thread "main" java.lang.NoSuchMethodException: AbstractTest$InnerTest.<init>(AbstractTest)
at java.lang.Class.getConstructor0(Class.java:2678)
at java.lang.Class.getConstructor(Class.java:1629)
at Test.main(Test.java:9)
答案 0 :(得分:7)
更新了问题更新
getConstructor()仅返回公共构造函数。您需要使用getDeclaredConstructors()来获取其他非公共构造函数
这有效:
import java.lang.reflect.Constructor;
public class Test extends AbstractTest
{
Test(int i){
super(i);
}
public static void main(String[] args) throws Exception
{
Test t = new Test(1);
final Constructor<?>[] constructors = Class.forName("AbstractTest$InnerTest").getDeclaredConstructors();
Constructor<?> noArgConstructor = null;
for (final Constructor<?> constructor : constructors)
{
if (constructor.getParameterTypes().length == 1)
noArgConstructor = constructor;
}
if (noArgConstructor == null)
throw new RuntimeException("Failed to find no arg constructor");
AbstractTest.InnerTest innerTest = (AbstractTest.InnerTest) noArgConstructor.newInstance(t);
innerTest.test();
}
}
abstract class AbstractTest{
AbstractTest(int i){}
class InnerTest
{
InnerTest(){
System.out.println("inner test class");
}
void test(){
System.out.println("inner test");
}
}
}
打印:
inner test class
inner test