[英]Java - working with list of objects and generics
I have a problem with a simple piece of Java code. 我有一段简单的Java代码有问题。 I cannot determine if it solves the original purpose;
我无法确定它是否解决了最初的目的; the guy that wrote it (yet unreachable) just told me that "an object that implements
IA
should be a container ( List
) of IB
-like objects”. At first sight I have considered it wrong, because of the strong constraint ( T extends IB<T>
) seems illogical, but the IDE compiler does not show any related error/warning. 编写该文件的人(但仍无法到达)只是告诉我“实现
IA
的对象应该是类似IB
的对象的容器( List
)”。乍一看,我认为它是错误的,因为它的约束很强( T extends IB<T>
)似乎不合逻辑,但是IDE编译器没有显示任何相关的错误/警告。
If such code is meaningful, could someone please provide an example of practical usage of such interfaces. 如果此类代码有意义,请有人提供此类接口的实际用法示例。
Thanks in advance. 提前致谢。
import java.util.List;
public interface IA<T extends IB<T>> {
public List<T> getList();
}
public interface IB<T> {
public T getValue();
}
UPDATE 1 : added test with concrete sample classes 更新1 :添加了带有具体样本类的测试
class Bar implements IA<Foo>{
List<Foo> list;
@Override
public List<Foo> getList() {
return list;
}
Bar(List<Foo> foos) {
this.list = foos;
}
}
class Foo implements IB<Foo> {
public Float data;
@Override
public Foo getValue() {
return foo;
}
Foo(Float data){
this.data = data;
}
public Float getV() {
return data;
}
}
public class DataTest {
@Test
public void myTest() {
Foo f = new Foo(10F);
List<Foo> fs = new ArrayList<>();
fs.add(f);
Bar bar = new Bar(fs);
List<Foo> foos = bar.getList();
System.out.println(foos.get(0).getV());
}
}
Is this the correct way to use IA and IB? 这是使用IA和IB的正确方法吗?
As T
is only used in covariant position, it is safe to use as it is, so the comment on IA
can be correct. 由于
T
仅在协变位置使用,因此按原样使用是安全的,因此对IA
的评论可能是正确的。 If IA
had a method accepting a T
(like int compare(T a, T b)
) in one of its parameters, that would cause problems as it were in a contravariant position. 如果
IA
具有在其参数之一中接受T
的方法(例如int compare(T a, T b)
),则由于它处于互变位置,因此会引起问题。
Such a constraint makes sense in certain circumstances. 在某些情况下,这样的约束是有意义的。 For example, if you want to make an sorted list class, you might do something like
例如,如果要创建排序列表类,则可以执行以下操作
class SortedList<T extends Comparable<? super T>>
where you require that the element type can be compared to itself, which is necessary for you to sort it. 需要将元素类型与自身进行比较的地方,这是对元素进行排序所必需的。 (Note that
Comparable
itself doesn't have a bound on its type parameter, just like here.) (请注意,
Comparable
本身在类型参数上没有限制,就像这里一样。)
The super
in the thing above is because Comparable
is a consumer with respect to T
, and so per PECS, you should use super
wildcards with Comparable
. 上面的
super
是因为Comparable
是T
的使用者,因此对于每个PECS,您应该使用Comparable
super
通配符。 In your case, since IB
is a producer with respect to T
, you could make it public interface IA<T extends IB<? extends T>>
在您的情况下,由于
IB
是T
的生产者,您可以使其成为public interface IA<T extends IB<? extends T>>
public interface IA<T extends IB<? extends T>>
if you want to make it most general. 如果要使其更通用,则
public interface IA<T extends IB<? extends T>>
。
As to an actual use case that uses this constraint, here's one I came up with that is a class that uses the constraint: 至于使用此约束的实际用例,我想到的是一个使用约束的类:
class Bar<T extends IB<T>> implements IA<T> {
T start;
@Override
public List<T> getList() {
List<T> result = new ArrayList<T>();
for (T x = start; x; x = x.getValue()) {
result.add(x);
}
return result;
}
Bar(T start) {
this.start = start;
}
}
Where you have an implementing class that it itself generic (with the same <T extends IB<T>>
bound), and it takes one T
and generates more T
s until it reaches null
, and returns a list of these. 如果您有一个自己本身是通用的实现类(具有相同的
<T extends IB<T>>
边界),并且它占用一个T
并生成更多T
直到达到null
,然后返回这些列表。
Though this still doesn't require that the interface IA
have the constraint, so I guess it still doesn't provide an example where the bound on the parameter of IA
is necessary. 尽管这仍然不要求接口
IA
具有约束,所以我想它仍然没有提供必须限制IA
参数的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.