我有2個類:SubEditor和Editor。上傳時出現ClassCastException
public class SubEditor extends Editor {
我創建一個副主編對象,並試圖將其轉換爲編輯器,但我會得到一個ClassCastException:
try {
(Editor) ClassLoader.getSystemClassLoader().loadClass("bla.SubEditor").newInstance();
} catch (Exception ex) {
try {
Object object = ClassLoader.getSystemClassLoader().loadClass("bla.SubEditor").newInstance();
// prints "false":
System.out.println(object instanceof Editor);
// prints "bla.SubEditor":
System.out.println(object.getClass().getCanonicalName());
// prints "Super: bla.Editor":
System.out.println("Super: "+object.getClass().getSuperclass().getCanonicalName());
} catch (Exception e) {
// do sth
}
}
這怎麼可能,我得到「假」上「SubEditor instanceof Editor」?
例外:
java.lang.ClassCastException: bla.SubEditor cannot be cast to bla.Editor
希望能幫到你!
問候,
馬丁
JSonEditor或副主編?這很混亂。 – 2013-03-05 18:22:51
對不起,我的意思是SubEditor。更新我的描述 – 2013-03-05 18:24:05