Java Generics有没有采用需要实现2个接口的Generic参数的方法? [英] Is there a way with Java Generics to take Generic parameter that requires implementation of 2 interfaces?
问题描述
说我有这个代码 -
public interface ParentInterface1 {
public List< ;?扩展ChildInterface1>的getChildren();
public void setChildren(List< ;? extends ChildInterface1> children);
}
public interface ParentInterface2 {
public List< ;?扩展ChildInterface2>的getChildren();
public void setChildren(List< ;? extends ChildInterface2> children);
}
public interface ChildInterface1 {
public String getField();
public void setField(String field);
}
public interface ChildInterface2 {
public String getField();
public void setField(String field);
}
公共类LParentImpl实现ParentInterface1,ParentInterface2 {
private List< ChildImpl>列表;
public List< ChildImpl> getChildren(){
返回列表;
}
public void setChildren(List< ...想要接受ChildImpl,其中
实现ChildInterface1和ChildInterface2>子元素){
抛出新的UnsupportedOperationException(还不支持。 );
}
}
public class ChildImpl implements ChildInterface1,ChildInterface2 {
private String field;
public String getField(){
return field;
}
public void setField(String field){
this.field = field;
$ b $ p有没有办法让setChildren()在ParentImpl类可以工作,而不需要从接口和实现中完全删除泛型类型?
我想做类似于 -
public void setChildren(List <?extends ChildInterface1& ChildInterface2> children)
这种接口/实现结构对于非泛型类型是有效的,但是似乎泛型的运行时擦除的某些方面可能会使这不可能?或者我错过了一些神奇的语法?
编辑:使用列表< ;?延伸ChildInterface1& ChildInterface2>产生这个编译错误 -
... \ParentImpl.java:20:>预计
public void setChildren(List <?extends ChildInterface1& ChildInterface2> children){
解决方案您的问题没有意义。
ParentInterface1.setChildren
接受列表与LT; ChildInterface1>
。因此,这么多 LParentImpl.setChildern
,但是你正试图限制它,所以它不会。
你可能会想要说,parameterise ParentInterface1
/ 2
,但我建议避免多接口的继承接口涉及泛型)。
Say I have this code -
public interface ParentInterface1 {
public List<? extends ChildInterface1> getChildren();
public void setChildren(List<? extends ChildInterface1> children);
}
public interface ParentInterface2 {
public List<? extends ChildInterface2> getChildren();
public void setChildren(List<? extends ChildInterface2> children);
}
public interface ChildInterface1 {
public String getField();
public void setField(String field);
}
public interface ChildInterface2 {
public String getField();
public void setField(String field);
}
public class LParentImpl implements ParentInterface1, ParentInterface2 {
private List<ChildImpl> list;
public List<ChildImpl> getChildren() {
return list;
}
public void setChildren(List<... wants to accept ChildImpl, which
implements ChildInterface1 & ChildInterface2> children) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
public class ChildImpl implements ChildInterface1, ChildInterface2 {
private String field;
public String getField() {
return field;
}
public void setField(String field) {
this.field = field;
}
}
Is there a way to make the setChildren() in the ParentImpl class work, without removing the Generic typing completely from the interfaces and implementation?
I'd like to do something like -
public void setChildren(List<? extends ChildInterface1 & ChildInterface2> children)
This sort of interface/implementation structure is valid for non Generic types, but it seems some aspect of the run-time erasure of Generics might make this impossible? Or am I missing some magic syntax?
Edit: Using the List<? extends ChildInterface1 & ChildInterface2> yields this compile error -
...\ParentImpl.java:20: > expected
public void setChildren(List<? extends ChildInterface1 & ChildInterface2> children) {
解决方案 Your problem doesn't makes sense.
ParentInterface1.setChildren
accepts List<ChildInterface1>
. Therefore so much LParentImpl.setChildern
, but you are trying to constrain it so that it does not.
You might want to say, parameterise ParentInterface1
/2
, but I'd suggest avoiding multiple inheritance of interface wherever possible (not just where generics are involved).
这篇关于Java Generics有没有采用需要实现2个接口的Generic参数的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!