这个是getScreenSize的实现好像是在rt.jar里面。这段解释可以看下,我觉得说的挺好的。Well, you really don't need to know what class is actually implementing the Toolkit (it's actually different for every OS), nor "how" it implements "getScreenSize()" - an abstract method defines that any class extending Toolkit must provide that method. The "getDefaultToolkit()" method returns a concrete implementation of Toolkit. Ergo, the "unknown" class returned from "getDefaultToolkit()" provides an implementation of "getScreenSize()."You actually see quite a lot of these types of patterns (cf. Builder, Factory) in Java. This allows the language and many programs programmed in Java to allow for many different implementations without having to worry about the exact implementing class (cf. polymorphism). Personally, I think it's rather elegant.
解决方案 »
- 64位win2008R2下,java内存溢出问题
- 用JBUILDER生成EXE文件后可以打开,可是要从旁边读取图片,想打包进EXE文件里...请点击
- 刚接触java,请教各位棘手一些问题!
- 请问各位,开发JSP和Servlet以及JavaBean并且用在Tomcat上面,要不要给Sun交什么授权费用阿?
- 求助:数组变换为什么出错?
- (●●●●,高分100)请各位大侠指教,在JBUILDER中如何连接MYSQL 数据库,详细些
- 如何去除批处理虚拟机产生的dos黑框?
- 菜鸟级问题!!
- 请问有谁对java.awt.dnd.*包比较熟,能大致讲一下吗!
- How can I get current system datetime?
- 这个程序跌代器用与不用的区别在哪里?
- JAVA短信猫碰问题,无法启动设备,抛出SMSLibException例外
if (toolkit == null) {
try {
// We disable the JIT during toolkit initialization. This
// tends to touch lots of classes that aren't needed again
// later and therefore JITing is counter-productiive.
java.lang.Compiler.disable();
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction() {
public Object run() {
String nm = null;
Class cls = null;
try {
nm = System.getProperty("awt.toolkit", "sun.awt.X11.XToolkit");
try {
cls = Class.forName(nm);
} catch (ClassNotFoundException e) {
ClassLoader cl = ClassLoader.getSystemClassLoader();
if (cl != null) {
try {
cls = cl.loadClass(nm);
} catch (ClassNotFoundException ee) {
throw new AWTError("Toolkit not found: " + nm);
}
}
}
if (cls != null) {
toolkit = (Toolkit)cls.newInstance();
if (GraphicsEnvironment.isHeadless()) {
toolkit = new HeadlessToolkit(toolkit);
}
}
} catch (InstantiationException e) {
throw new AWTError("Could not instantiate Toolkit: " + nm);
} catch (IllegalAccessException e) {
throw new AWTError("Could not access Toolkit: " + nm);
}
return null;
}
});
loadAssistiveTechnologies();
} finally {
// Make sure to always re-enable the JIT.
java.lang.Compiler.enable();
}
}
return toolkit;
}
这个实现里面返回的toolkit虽然定义使用的java.awt.Toolkit,但是在new的时候是new的它的子类了,所以楼主在调用这个方法的时候会调用哪个子类的方法,就看这个实现里面是new的哪个子类了
if (toolkit == null) {
try {
// We disable the JIT during toolkit initialization. This
// tends to touch lots of classes that aren't needed again
// later and therefore JITing is counter-productiive.
java.lang.Compiler.disable();
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction() {
public Object run() {
String nm = null;
Class cls = null;
try {
nm = System.getProperty("awt.toolkit", "sun.awt.X11.XToolkit");
try {
cls = Class.forName(nm);
} catch (ClassNotFoundException e) {
ClassLoader cl = ClassLoader.getSystemClassLoader();
if (cl != null) {
try {
cls = cl.loadClass(nm);
} catch (ClassNotFoundException ee) {
throw new AWTError("Toolkit not found: " + nm);
}
}
}
if (cls != null) {
toolkit = (Toolkit)cls.newInstance();
if (GraphicsEnvironment.isHeadless()) {
toolkit = new HeadlessToolkit(toolkit);
}
}
} catch (InstantiationException e) {
throw new AWTError("Could not instantiate Toolkit: " + nm);
} catch (IllegalAccessException e) {
throw new AWTError("Could not access Toolkit: " + nm);
}
return null;
}
});
loadAssistiveTechnologies();
} finally {
// Make sure to always re-enable the JIT.
java.lang.Compiler.enable();
}
}
return toolkit;
}