[英]How to extend objects contained in a List?
I have a List named resourceItems which containes ResourceItem objects. 我有一个名为resourceItems的列表,其中包含ResourceItem对象。
public class ResourceItem {
private Long id;
private String name;
public ResourceItem(Long id, String name) {
this.id = id;
this.name = name;
}
// getters and setters...
}
public class SomeClass {
private List<ResourceItem> resourceItems = FindAllResourcesWebSerbice();
}
I would like to extend the objects in the List to include a boolean field named selected. 我想扩展列表中的对象,以包括一个名为selected的布尔字段。
I've tried several variations of classes that extends ResourceItem (see below) including options using generics, but have not been successful. 我尝试了几种扩展ResourceItem(参见下文)的类的变体,包括使用泛型的选项,但没有成功。 I would love a solution that uses generics for reuse.
我希望使用泛型进行重用的解决方案。
public class ExtendedResourceItem extends ResourceItem {
private boolean selected = false;
public ExtendedResourceItem() {
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
public class SomeClass {
private List<ResourceItem> resourceItems = FindAllResourcesWebSerbice();
private List<ExtendedResourceItem> extendedResourceItem = resourceItems;
}
Any help is much appreciated. 任何帮助深表感谢。
Assuming ResourceItem
implements equals
and hashCode
correctly, why not just keep a set of the ones that are selected: 假设
ResourceItem
正确实现equals
和hashCode
,为什么不保留一组选定的对象:
Set<ResourceItem> selectedResourceItems = new HashSet<ResourceItem>();
Then you could add
an item to the set when it's selected, remove
it when it's deselected, and check to see if the set contains
one when you need to know if it's selected. 然后,您可以在选中某个项目时将其
add
到该集合中,而在取消选中该项目时将其remove
,并在需要知道是否选中该集合时检查该集合contains
是否contains
一个项目。
This solution is very similar to Michael Myers' comment about using a Map<ResourceItem, Boolean>
. 此解决方案与Michael Myers关于使用
Map<ResourceItem, Boolean>
的评论非常相似。
You should use composition, not inheritance here. 您应该在这里使用组合,而不是继承。 From your description it sounds like
ResourceItem
is a domain object, but on screen, you should wrap it 从您的描述中听起来,
ResourceItem
是一个域对象,但是在屏幕上,您应该包装它
class ScreenResourceItem {
ResourceItem item;
boolean selected;
}
Create a new list of ScreenResourceItem
and put all the wrapped ResourceItem
s in it. 创建一个新的
ScreenResourceItem
列表,并将所有包装的ResourceItem
放入其中。
Generics and list covariance are not relevant here. 泛型和列表协方差在这里不相关。
But using your snippet: 但是使用您的代码段:
private List<ResourceItem> resourceItems = ... \\ some assignment;
private List<ExtendedResourceItem> extendedResourceItems = resourceItems;
is absolutely wrong. 是绝对错误的。 Since not any ResourceItem is guarantee to be also ExtendedResourceItem.
由于没有任何ResourceItem可以保证也是ExtendedResourceItem。 (resourceItems list can contain objects that are ResourceItem but are NOT ExtendedResourceItem).
(resourceItems列表可以包含ResourceItem但不是ExtendedResourceItem的对象)。
I don't see the point you want to reach, but if You want to transform resourceItems into list of resourceItems that have selected
attribute, what about this concept: 我看不到要达到的目标,但是如果您想将resourceItems转换为已
selected
属性的resourceItems列表,那么这个概念呢?
public class ExtendedResourceItem {
private ResourceItem item = null;
private boolean selected = false;
public ExtendedResourceItem(ResourceItem item, boolean selected) {
this.item = item;
this.selected = selected;
}
// ... getters and setters
}
public class SomeClass {
private List<ResourceItem> resourceItems = FindAllResourcesWebSerbice();
private List<ExtendedResourceItem> extendedResourceItems = new ArrayList<ExtendedResourceItem>();
for (ResourceItem item: resourceItems) {
extendedResourceItems.add(new ExtendedResourceItem(item, false));
}
}
Is it the generics limits that are confusing? 泛型限制令人困惑吗? Try using the Upper Bounded Wildcards , eg
尝试使用上界通配符 ,例如
public class SomeClass {
private List<ExtendedResourceItem> extendedResourceItems = FindAllResourcesWebSerbice();
private List<? extends ResourceItem> resourceItems = extendedResourceItems;
}
Alternatively: 或者:
public class SomeClass {
private List<? extends ExtendedResourceItem> extendedResourceItems = FindAllResourcesWebSerbice();
private List<? extends ResourceItem> resourceItems = extendedResourceItems;
}
More information about wildcards Wildcards and Subtyping . 有关通配符和通配符的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.