Java集合:将子集合传递为父集合
假设我有一个接口和一些类:
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,当我调用时
final LocalizedButtonsPanel localizedButtonsPanel = new LocalizedButtonsPanel();
final Set<LocalizedButton> localizedButtonsSet = new LinkedHashSet<LocalizedButton>();
localizedButtonsPanel.addComponents(localizedButtonsSet);
,我发现此方法不适用于此参数。 如果我尝试在 LocalizedButtonsPanel
中将此方法重载为 addComponents(Set
,当然,我会得到类型擦除。
可能会遗漏某些模式,或者存在处理此架构以实现正确添加 LocalizedButtons 集的技巧?
我已经得到了答案,我想让我的示例更加具体 - 我的实现中有一些验证器,所以我需要将集合类型也存储为通用类型,这是我使用答案得到的简化代码:
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(); }
}
并且在这种情况下,它有效
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(); }
}
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);
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.
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(); }
}
And in this case, it works
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更改 addComponents() 签名,以便
该方法接受 Button 的子类集。
这样,您就可以将
Set
作为参数传递,因为LocalizedButton
扩展了Button
,因此与的参数 Type 匹配设置
。Change the addComponents() signature to
so that the methods accepts sets of subclasses of Button.
This way, you can pass a
Set<LocalizedButton>
as an argument, becauseLocalizedButton
extendsButton
and therefore matches the parameter Type ofSet<? extends Button>
.您
应该
仅为按钮类型创建列表,而不为扩展该类型的对象创建列表。
You have
Should be
The list is created only for the type of button not for Object that extend that type.
Java 中参数化类型不是协变的。这很好,否则您将能够将 Dog 添加到列表中。已升级为 List。您可以在使用站点添加协方差,例如 List可以分配一个List并且你不能调用它的 add 方法。
Parameterised types are not covariant in Java. This is good, otherwise you would be able to add a Dog to a List<Cat> that had been upcast to List<Animal>. You can add covariance at the usage site, e.g., List<? extends Animal> can be assigned a List<Cat> and you can't call its add method.