我有一個投問題,其產生java.lang.ClassCastException
例外:java.lang.ClassCastException與org.apache.logging.log4j.Logger
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.message.MessageFactory;
public class MyLogger extends org.apache.logging.log4j.core.Logger {
MyLogger(LoggerContext context, String name, MessageFactory messageFactory) {
super(context, name, messageFactory);
// TODO Auto-generated constructor stub
}
public static MyLogger getLogger(String name) {
org.apache.logging.log4j.core.Logger logger_ = (org.apache.logging.log4j.core.Logger) LogManager
.getLogger(name);
return (MyLogger) logger_;
}
}
雖然這是同樣的原則,因爲這一個:
public class test {
private static class A {
}
private static class B extends A {
}
public static void main(String [] args) {
A a = new A();
B b = new B();
a = (A) b;
}
}
有人可以解釋一下嗎?
你確定'LogManager.getLogger(name)'返回'MyLogger'的一個實例嗎?爲什麼呢? – sp00m 2014-10-30 14:12:00