Java:子类化 ResourceBundle

发布于 2024-11-30 02:24:21 字数 425 浏览 3 评论 0原文

假设我有一些 ResourceBundle 实例:

ResourceBundle bundle = getBundle();
... 
    some more code that does stuff with bundle
...

我想知道 bundle 是否有特定的键。不幸的是,我使用的所有方法(containsKey()、keySet() 等)还会检查父包中的密钥。我想要使​​用的方法是handleKeySet(),它是受保护的,因此不可见。为了解决这个问题,我能想到的唯一解决方案是创建ResourceBundle的子类并实现getKeys(),使其仅返回当前包的键并排除父包的键。我开始怀疑这个解决方案的部分可能是由于我对继承的理解混乱。我的问题是..这似乎是正确的方法吗?如果是这样,任何提示或推动正确的方向将不胜感激。

Say I have some instance of ResourceBundle:

ResourceBundle bundle = getBundle();
... 
    some more code that does stuff with bundle
...

I want to know if bundle has a particular key. Unfortunately, all of the methods I would use (containsKey(), keySet(), etc.) also check the parent bundle for the key. The method I would want to use is handleKeySet(), which is protected, therefore not visible.To get around this issue, the only solution I can think of is to create a subclass of ResourceBundle and implement getKeys() such that it returns only the keys of the current bundle and excludes the keys of the parent.The part where I start to doubt this solution is probably due to my confused understanding of inheritance. My question is.. does this seem to be the right way to go? And if so, any hints or a push in the right direction would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

乄_柒ぐ汐 2024-12-07 02:24:21

理论上,当您创建子类时,您不会改变继承的行为,您所做的只是通过使其更适合您的需求来“改进”它。

例如,您可以将 GregorianCalendar 扩展到 MyGregCal,以便使用它来计算与给定日期相对应的黄道带符号。但您应该避免为了计算儒略历等目的而改变它。为什么?因为每个接受 GregorianCalendar 的方法都将接受 MyGregCal,并期望它提供 GregorianCalendar 的功能。如果不这样做,那么坏事(更糟糕的是,意外坏事)就会随处发生。

因此,如果您无法在不破坏父类的契约的情况下获得所需的功能,那么您应该寻找其他地方。从头开始编写类,或者从更简单的父类(Properties?)

In theory, when you create a subclass you do not alter the behavior inheritated, what you do is "improve" it by making it more specific to your needs.

For example, you could extend GregorianCalendar into MyGregCal in order to use it to calculate the zodiacal sign corresponding to a given date. But you should avoid altering it in order to, say, calculate the julian calendar. Why? Because every method that accepts a GregorianCalendar will accept a MyGregCal, and will expect that it provides the functionality of GregorianCalendar. If it does not, then bad things (worse, unexpected bad things) can happen everywhere.

So, if you can not get the functionality that you need without breaking the contract of the parent class, you should look somewhere else. Write the class from scratch, or from a simpler parent class (Properties?)

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