【发布时间】:2010-05-24 04:07:10
【问题描述】:
私有静态 Importable getRightInstance(String s) 抛出异常 { 类 c = Class.forName(s).asSubclass(Importable.class); 可导入 i = c.newInstance(); 返回我; }
我也可以写
private static Importable getRightInstance(String s) throws Exception {
Class<? extends Importable> c = (Class<? extends Importable>)Class.forName(s);
Importable i = c.newInstance();
return i;
}
或
private static Importable getRightInstance(String s) throws Exception {
Class<?> c = Class.forName(s);
Importable i = (Importable)c.newInstance();
return i;
}
其中 Importable 是一个接口,s 是一个表示实现类的字符串。 好吧,无论如何,它给出了以下内容:
Exception in thread "main" java.lang.IncompatibleClassChangeError: class C1 has
interface Importable as super class
这是堆栈跟踪的最后一个 sn-p:
at java.lang.Class.forName(Class.java:169)
at Importer.getRightImportable(Importer.java:33)
at Importer.importAll(Importer.java:44)
at Test.main(Test.java:16)
现在,C1 类实际上实现了 Importable,我完全不明白它为什么抱怨。
提前致谢。
【问题讨论】:
标签: java