Java:通用继承/转换(ResourceBundle)
我收到一些 CastClassExceptions。我认为我对子类化的理解很混乱,需要一些澄清。我有以下类:
// defined in java.util
abstract class ResourceBundle {
...
}
// defined in java.util
class PropertyResourceBundle extends ResourceBundle {
...
}
// defined by myself
class ResourceBundleWrapper extends ResourceBundle {
...
// abstract in ResourceBundle
@Override public Enumeration<String> getKeys() {
throw new UnsupportedOperationException();
}
// abstract in ResourceBundle
@Override protected Object handleGetObject(String key) {
throw new UnsupportedOperationException();
}
// protected in ResourceBundle
public Set<String> handleKeySet() {
...
the code from ResourceBundle.handleKeySet()
...
}
...
}
包装类的目的是公开 handleKeySet() 方法,以便我可以获得捆绑包的密钥,而无需另外获取其父捆绑包的密钥。 我大致有以下代码:
ResourceBundle bundle = getBundle(); // method can return any subclass of ResourceBundle
我希望能够通过将 bundle 转换为 ResourceBundleWrapper 来获取它的密钥。我无法将包强制转换为 ResourceBundleWrapper 类型而不引发 ClassCastException。错误消息示例:
java.util.PropertyResourceBundle cannot be cast to com.common.ResourceBundleWrapper
我明白为什么会发生此强制转换异常。这不是我需要澄清的。我如何通过这种方法实现我想要实现的目标?
I am getting some CastClassExceptions. I think my understanding of subclassing in confused and could use some clarification. I have the following classes:
// defined in java.util
abstract class ResourceBundle {
...
}
// defined in java.util
class PropertyResourceBundle extends ResourceBundle {
...
}
// defined by myself
class ResourceBundleWrapper extends ResourceBundle {
...
// abstract in ResourceBundle
@Override public Enumeration<String> getKeys() {
throw new UnsupportedOperationException();
}
// abstract in ResourceBundle
@Override protected Object handleGetObject(String key) {
throw new UnsupportedOperationException();
}
// protected in ResourceBundle
public Set<String> handleKeySet() {
...
the code from ResourceBundle.handleKeySet()
...
}
...
}
The purpose of the wrapper class is to expose the handleKeySet() method so that I can get the keys of a bundle without in addition, getting the keys of its parent bundle.
I have roughly the following code:
ResourceBundle bundle = getBundle(); // method can return any subclass of ResourceBundle
I would like to be able to get the keys of bundle by casting it to a ResourceBundleWrapper. I cannot cast bundle to type ResourceBundleWrapper without getting a ClassCastException. Example error message:
java.util.PropertyResourceBundle cannot be cast to com.common.ResourceBundleWrapper
I understand why this cast exception is happening. That is not what I need clarification on. How do I achieve what I am trying to achieve with this approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PropertyResourceBundle 和 ResourceBundleWrapper 都是 ResourceBudle 的后代,因此它们都可以完成 ResourceBundle 的工作。然而,没有理由认为它们会取代
彼此。因此您尝试的演员阵容无效。
您可以让 PropertyResourceBundle 扩展 ResourceBundleWrapper 吗?然后您的类就可以从包装器的附加功能中受益。
The PropertyResourceBundle and ResourceBundleWrapper are each descendents of ResourceBudle, so they can each do ResourceBundle's job. However there is no reason to suppose that they replacements for
each other. So the cast you are attempting is invalid.
Could you instead have PropertyResourceBundle extend ResourceBundleWrapper? Then your class can benefit from the additional capabilities of the wrapper.
PropertyResourceBundle 和 ResourceBundleWrapper 都是 ResourceBundele。
Thnik 的班级有父类 Fruit,有 Mango 和 Kiwi 作为子类。
芒果不是猕猴桃:)猕猴桃也不是芒果,但两者都是水果:)
PropertyResourceBundle and ResourceBundleWrapper are both ResourceBundeles.
Thnik on a class that is has parent Fruit and has Mango and Kiwi as child classes.
Mango is not Kiwi :) and Kiwi is not Mango but both are fruits :)