Java集合:将子集合作为父集合 [英] Java Collections: Pass collection of children as collection of parents

查看:159
本文介绍了Java集合:将子集合作为父集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个接口和一些类:

Say I have an interface and some classes:

public interface IPanel<ComponentType extends Component> {
   public void addComponents(Set<ComponentType> components);
   public ComponentType create();
}

public class Button extends Component { }

public class LocalizedButton extends Button { }

public class ButtonsPanel implements IPanel<Button> {
    public void addComponents(Set<Button> components) { ... /* uses create() */ ; }
    public Button create() { return new Button(); }
}

public class LocalizedButtonsPanel extends ButtonsPanel {
    public Button create() { return new LocalizedButton(); }
}

然后我有一组LocalizedButtons,当我调用

Then I have a set of LocalizedButtons and when I call

final LocalizedButtonsPanel localizedButtonsPanel = new LocalizedButtonsPanel();
final Set<LocalizedButton> localizedButtonsSet = new LinkedHashSet<LocalizedButton>();
localizedButtonsPanel.addComponents(localizedButtonsSet);

我得到这个方法不适用于这个参数。
如果我尝试重载此方法作为 LocalizedButtonsPanel 中的 addComponents(Set< LocalizedButton>按钮)获取类型擦除,当然。

I get that this method is not applicable for this arguments. If I try to overload this method as addComponents(Set<LocalizedButton> buttons) in LocalizedButtonsPanel, I get type erasure, of course.

可能是一些模式错过了或者存在的技巧来处理这个架构来实现正确添加的LocalizedButtons集合

May be some pattern is missed or the trick exists to deal with this architecture to implement correct adding the set of LocalizedButtons?

我有答案,我想让我的例子更具体 - 我的实现中有一些验证器,所以我需要集合类型也被存储为通用的,这是简化的代码我已经使用答案:

I've got the answer and I want to make my example more concrete - I have some validators in my implementation, so I need the collection type to be also stored as generic, that is simplified code I've got using the answer:

public interface IPanel<ComponentType extends Component, CollectionType extends Collection<? extends Component>> extends Validated<CollectionType> {
   public void addComponents(CollectionType components);
   public ComponentType create();
}

public class Button extends Component { }

public class LocalizedButton extends Button { }

public class ButtonsPanel implements IPanel<Button, Set<? extends Button>> {
    public void addComponents(Set<? extends Button> components) { ... /* uses create() */ ; }
    public Button create() { return new Button(); }
}

public class LocalizedButtonsPanel extends ButtonsPanel {
    public Button create() { return new LocalizedButton(); }
}

在这种情况下,它工作

推荐答案

将addComponents()签名更改为

Change the addComponents() signature to

public void addComponents(Set<? extends Button> components)

,以便这些方法接受Button的子类集。
这样,您可以传递 Set< LocalizedButton> 作为参数,因为 LocalizedButton c $ c> Button ,因此匹配参数Type Set< ;?扩展Button>

so that the methods accepts sets of subclasses of Button. This way, you can pass a Set<LocalizedButton> as an argument, because LocalizedButton extends Button and therefore matches the parameter Type of Set<? extends Button>.

这篇关于Java集合:将子集合作为父集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆