Java:通用继承/转换(ResourceBundle)

发布于 2024-11-29 23:56:55 字数 1254 浏览 1 评论 0原文

我收到一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

隔纱相望 2024-12-06 23:56:55

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.

夏至、离别 2024-12-06 23:56:55

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文