GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] names = g.getAvailableFontFamilyNames();
for(String s:names)
   System.out.println(s);
以上可以输出所有本地支持的字体,困惑的是:
public static synchronized GraphicsEnvironment getLocalGraphicsEnvironment()
public abstract String[] getAvailableFontFamilyNames();
以上为JDK的GraphicsEnvironment类的源代码,
一个抽象类的引用调用该类的抽象方法,这是什么语法,不解。。抽象类抽象方法

解决方案 »

  1.   

    GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnvironment();
    抽象类的引用,指向实现了该抽象类的一个对象,该对象是由方法getLocalGraphicsEnvironment()实现的,目的是为了确保 单例模式(即只有一个该类的对象)!既然该类已经被实现了,第二个疑问也就没了……
      

  2.   

    谢谢解答,解决了我一部分疑问,给了我很多思路,但是还是有些问题没解决。
    public static synchronized GraphicsEnvironment getLocalGraphicsEnvironment()
    {
    if(localEnv == null)
            {
                String s = (String)AccessController.doPrivileged(new GetPropertyAction("java.awt.graphicsenv", null));
                try
                {
                    localEnv = (GraphicsEnvironment)Class.forName(s).newInstance();
                    if(isHeadless())
                        localEnv = new HeadlessGraphicsEnvironment(localEnv);
                }
                catch(ClassNotFoundException classnotfoundexception)
                {
                    throw new Error((new StringBuilder()).append("Could not find class: ").append(s).toString());
                }
                catch(InstantiationException instantiationexception)
                {
                    throw new Error((new StringBuilder()).append("Could not instantiate Graphics Environment: ").append(s).toString());
                }
                catch(IllegalAccessException illegalaccessexception)
                {
                    throw new Error((new StringBuilder()).append("Could not access Graphics Environment: ").append(s).toString());
                }
            }
            return localEnv;
    }
    其中localEnv被定义为:private static GraphicsEnvironment localEnv;
    localEnv引用指向的是被new出来的对象或newInstance出来的对象,所以不应该是单例模式吧。