sweat89 2012-08-17 22:26
浏览 277
已采纳

ResourceBundle 类getObject,在里面调用了抽象方法

[code="java"]
public final Object getObject(String key)。
从此资源包或它的某个父包中获取给定值的对象。此方法首先尝试使用 handleGetObject 从此资源包中获取对象。
[/code]

然后再看下handleGetObject 方法:
protected abstract Object handleGetObject(String key)

居然是抽象方法啊,怎么能使用呢???
小弟很是迷惑啊,求大神慷慨点拨

  • 写回答

1条回答 默认 最新

  • jinnianshilongnian 2012-08-18 09:06
    关注

    1获取ResourceBundle使用
    ResourceBundle myResources =
    ResourceBundle.getBundle("MyResources", currentLocale);

    2、getBundle方法实现:
    [code="java"]public static final ResourceBundle getBundle(String baseName)
    {
    return getBundleImpl(baseName, Locale.getDefault(),
    /* must determine loader here, else we break stack invariant */
    getLoader(),
    Control.INSTANCE);
    }[/code]

    可以看到又调用了getBundleImpl:通过一系列调用,最终调用:
    [code="java"] public ResourceBundle newBundle(String baseName, Locale locale, String format,
    ClassLoader loader, boolean reload)
    throws IllegalAccessException, InstantiationException, IOException {
    String bundleName = toBundleName(baseName, locale);
    ResourceBundle bundle = null;
    if (format.equals("java.class")) {
    try {
    Class<? extends ResourceBundle> bundleClass
    = (Class<? extends ResourceBundle>)loader.loadClass(bundleName);

            // If the class isn't a ResourceBundle subclass, throw a
            // ClassCastException.
            if (ResourceBundle.class.isAssignableFrom(bundleClass)) {
            bundle = bundleClass.newInstance();
            } else {
            throw new ClassCastException(bundleClass.getName()
                     + " cannot be cast to ResourceBundle");
            }
        } catch (ClassNotFoundException e) {
        }
        } else if (format.equals("java.properties")) {
        final String resourceName = toResourceName(bundleName, "properties");
        final ClassLoader classLoader = loader;
        final boolean reloadFlag = reload;
        InputStream stream = null;
        try {
            stream = AccessController.doPrivileged(
            new PrivilegedExceptionAction<InputStream>() {
                public InputStream run() throws IOException {
                InputStream is = null;
                if (reloadFlag) {
                    URL url = classLoader.getResource(resourceName);
                    if (url != null) {
                    URLConnection connection = url.openConnection();
                    if (connection != null) {
                        // Disable caches to get fresh data for
                        // reloading.
                        connection.setUseCaches(false);
                        is = connection.getInputStream();
                    }
                    }
                } else {
                    is = classLoader.getResourceAsStream(resourceName);
                }
                return is;
                }
            });
        } catch (PrivilegedActionException e) {
            throw (IOException) e.getException();
        }
        if (stream != null) {
            try {
            bundle = new PropertyResourceBundle(stream);
            } finally {
            stream.close();
            }
        }
        } else {
        throw new IllegalArgumentException("unknown format: " + format);
        }
        return bundle;
    }[/code]
    

    即默认情况下最终会调用PropertyResourceBundle 进行实例化。

    这是一个简单工厂 设计模式:调用getBundle时 选择实现

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器