这个是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. 

解决方案 »

  1.   

    下面是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);
                                        }
                                    }
                                }
                                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的哪个子类了
      

  2.   

    在发一次实现代码,上面有点小错误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);
                                        }
                                    }
                                }
                                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;
        }