[code="java"]
public final Object getObject(String key)。
从此资源包或它的某个父包中获取给定值的对象。此方法首先尝试使用 handleGetObject 从此资源包中获取对象。
[/code]
然后再看下handleGetObject 方法:
protected abstract Object handleGetObject(String key)
居然是抽象方法啊,怎么能使用呢???
小弟很是迷惑啊,求大神慷慨点拨
[code="java"]
public final Object getObject(String key)。
从此资源包或它的某个父包中获取给定值的对象。此方法首先尝试使用 handleGetObject 从此资源包中获取对象。
[/code]
然后再看下handleGetObject 方法:
protected abstract Object handleGetObject(String key)
居然是抽象方法啊,怎么能使用呢???
小弟很是迷惑啊,求大神慷慨点拨
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时 选择实现