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

YSS_YSS2013 2013-12-11 04:23:19
以下程序片段用于获取当前屏幕尺寸:
Toolkit kit=new Toolkit.getDefaultToolkit();
Dimension screenSize=kit.getScreenSize();

请教各位大虾,getScreenSize()是抽象方法,并没有定义功能,怎么还可以调用?这里是不是调用了Toolkit的子类中覆写的getScreenSize()?如果是的话,这里的子类是哪个?
...全文
398 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
少羽 2013-12-11
  • 打赏
  • 举报
回复
在发一次实现代码,上面有点小错误 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; }
少羽 2013-12-11
  • 打赏
  • 举报
回复
下面是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的哪个子类了
teemai 2013-12-11
  • 打赏
  • 举报
回复
这个是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.

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧