Java 最佳实践:将子类对象放入需要超类对象的 HashMap 中

发布于 2024-12-02 06:26:14 字数 809 浏览 2 评论 0原文

假设我用 SuperClass 作为值类型实例化一个 HashMap。然后,我将 SubClass 对象作为值添加到 Map。当我从 Map 检索这些值时,它们作为 SuperClass 类型的对象返回,我将其显式转换回 Subclass

class SuperClass {}

class SubClass1 extends SuperClass { int one;}

class SubClass2 extends SuperClass { int two;}

class DoSomething {

   DoSomething() {
      Map<String, SuperClass> map = new HashMap<String, SuperClass>();

      map.put("1", new SubClass1());
      map.put("2", new SubClass2());

      SubClass1 one = (SubClass1) map.get("1");
   }
}

我需要知道返回的对象是特定的子类,因为我想访问仅存在于子类中的方法。如果返回的类型可以是任意数量的不同子类,那么使用 instanceof 是否被认为是确定类型和转换的最佳实践?

SuperClass s = map.get("1");
if (s instanceof SubClass1) {
  (SubClass1)s.one = 1;
}

谢谢

Let's say I instantiate a HashMap with SuperClass as value-type. I then add SubClass objects as values to the Map. When I retrieve those values from the Map, they are returned as objects of type SuperClass, which I explicitly cast back to Subclass:

class SuperClass {}

class SubClass1 extends SuperClass { int one;}

class SubClass2 extends SuperClass { int two;}

class DoSomething {

   DoSomething() {
      Map<String, SuperClass> map = new HashMap<String, SuperClass>();

      map.put("1", new SubClass1());
      map.put("2", new SubClass2());

      SubClass1 one = (SubClass1) map.get("1");
   }
}

I need to know that the returned object is of the specific SubClass because I want to access methods that only exist in the SubClass. If the returned type can be any number of different SubClasses, is the use of instanceof considered best practice in determining the type and the casting?

SuperClass s = map.get("1");
if (s instanceof SubClass1) {
  (SubClass1)s.one = 1;
}

Thanks

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

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

发布评论

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

评论(3

余生一个溪 2024-12-09 06:26:15

最佳实践应该是将每个子类类型放入不同的映射中。

在执行强制转换之前使用instanceof,如果您确实需要执行该强制转换,这是一个好主意,因为这样您将防止出现ClassCastException。

请注意,如果您的代码有很多 instanceof 指令,那么您的设计可能会很糟糕。

如果您想将它们放在同一个 Map 中,那么您需要考虑您的设计:

让您的 DoSomething 类了解不同的 SubClass 类型以执行特定操作?我看到 3 种可能性:

  1. 是的,DoSomething 必须了解您的所有子类类型。然后不用担心,使用 instanceof 执行检查并转换从映射中检索的对象,或者更好地将它们存储到不同的映射中。
  2. 不, DoSomething 不需要知道不同的 SubClass 因为可以使用它们抛出公共接口 SuperClass 。这是很好的设计。
  3. 您不希望 DoSomething 知道不同的子类类型,但在某些情况下您觉得需要使用一些子类特定的方法:重构您的代码,您的设计错误。

The best practice should be putting each SubClass type inside a different Map.

Using instanceof before performing a cast, if you really need to do that cast, it's a good idea because this way you will prevent a ClassCastException.

Pay attention that if your code has a lot of instanceof directives then you could have a bad design.

If you want to put them in the same Map then you need to think about your design:

have your DoSomething class to be aware of the differents SubClass types to perform specific operation?I see 3 possibilities:

  1. Yes, DoSomething must be aware of all your SubClass types. Then don't worry, perform your check with instanceof and cast the object retrieved from the map or, better, store them into different Maps.
  2. No, DoSomething doesn't need to be aware of the different SubClass because can use them throw the common interface SuperClass . That's good design.
  3. You don't want DoSomething to be aware of different SubClass types, but in certain situation you feel the needs to use some subclass specific methods: refactor your code, you have a wrong design.
唯憾梦倾城 2024-12-09 06:26:15

是的,你绝对应该使用instanceof来保证类型安全。否则,您如何知道您提取的对象实际上是否是正确的子类?

Yes, you should definitely use instanceof to be typesafe. Otherwise, how would you know whether or not the object you pulled out is in fact the correct subclass?

临走之时 2024-12-09 06:26:14

根据具体情况,有多种方法:

  • 在超类中添加抽象方法来执行相关操作。
  • 使用适配器作为映射值的类型。添加条目时,使用适配器的特化来匹配子类型。
  • 单独的地图。如果他们一开始就不应该在同一张地图上,那就特别好。

There are a number of ways about this depending upon the particular situation:

  • Add an abstract method to the superclass for performing the relevant operation.
  • Use an adapter as the type of the map values. When adding the entries, use a specialisation of the adapter to match the subtype.
  • Separate maps. Particularly good if they shouldn't have been in the same map in the first place.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文