当前位置: 代码迷 >> J2SE >> 关于抽象方法Toolkit.getScreenSize()的疑惑
  详细解决方案

关于抽象方法Toolkit.getScreenSize()的疑惑

热度:32   发布时间:2016-04-23 21:21:08.0
关于抽象方法Toolkit.getScreenSize()的困惑
以下程序片段用于获取当前屏幕尺寸:
Toolkit kit=new Toolkit.getDefaultToolkit();
Dimension screenSize=kit.getScreenSize();

请教各位大虾,getScreenSize()是抽象方法,并没有定义功能,怎么还可以调用?这里是不是调用了Toolkit的子类中覆写的getScreenSize()?如果是的话,这里的子类是哪个?

------解决方案--------------------
下面是kit.getScreenSize()方法的实现

public static synchronized Toolkit getDefaultToolkit() {
        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);
                                    }
  相关解决方案