Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java泛型是否有方法获取需要实现2个接口的泛型参数?_Java_Generics_Interface - Fatal编程技术网

Java泛型是否有方法获取需要实现2个接口的泛型参数?

Java泛型是否有方法获取需要实现2个接口的泛型参数?,java,generics,interface,Java,Generics,Interface,假设我有这个密码- public interface ParentInterface1 { public List<? extends ChildInterface1> getChildren(); public void setChildren(List<? extends ChildInterface1> children); } public interface ParentInterface2 { public List<? exte

假设我有这个密码-

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;
    }
}
公共接口ParentInterface1{
公共列表您可以执行以下操作:

public class MyClass<T extends ChildInterface1 & ChildInterface2> { ... }
公共类MyClass{…}

看看你的问题没有道理

ParentInterface1.setChildren
接受
List
。因此有很多
LParentImpl.setChildren
,但您试图约束它,使其不受约束


您可能想说,参数化
ParentInterface1
/
2
,但我建议尽可能避免接口的多重继承(不仅仅是涉及泛型的情况)。

您可以指定一个方法,该方法采用实现这两个接口的对象,如下所示:

public <T extends IFirst & ISecond> void doSomething(T obj) {}
publicsvoiddosomething(tobj){}

但是,在您的示例中,这并不重要,因为您的两个子接口都指定了相同的方法。

这不是答案,但我必须感谢Jorn的答案

当我试图完全按照你的答案回答我的时候,我偶然发现了这条线索(尽管它没有回答最初的提问者)。我试图这样做:

public static interface Displayable
    extends Identifiable, Nameable { }

public void print(Displayable obj) {
    System.out.println("[" + obj.getIdentity() + "] " + obj.getName());
}
这不起作用,因为我传递的对象没有实现可显示,但不能单独命名和识别

使用Jorn的技术,我做到了:

public <T extends Identifiable & Nameable> void print(T obj) {
    System.out.println("[" + obj.getIdentity() + "] " + obj.getName());
}
公共作废打印(T obj){
System.out.println(“[”+obj.getIdentity()+“]”+obj.getName());
}
而且它工作得完美无缺


我希望这将有助于其他与我有类似问题的人(并且偶然发现了这个问题)。

你是对的。我试图解决一个事实,即你不能同时满足-public void setChildren(List)public void setChildren(List)的两个接口因为擦除-但是我认为我在这里尝试做的是不可能的,因为语言的机制。你不能让两个方法都重载,因为语言规范(重载是一个静态的问题,所以即使擦除也是可能的)。该方法最严格的参数类型是类似于
list的内容。它几乎是有效的。请参阅我的答案。谢谢你,Jorn!我在尝试完全按照你的答案进行操作时偶然发现了这条线索(尽管它没有回答最初的提问者).I尝试这样做:公共静态接口可显示扩展可识别、可命名{}公共无效打印(可显示obj){System.out.println(“[”+obj.getIdentity()+“]”“+obj.getName());}这不起作用,因为我传递的对象没有实现可显示,但不能单独命名和识别。您刚刚阻止我单击“发布您的问题”。谢谢!。
public static interface Displayable
    extends Identifiable, Nameable { }

public void print(Displayable obj) {
    System.out.println("[" + obj.getIdentity() + "] " + obj.getName());
}
public <T extends Identifiable & Nameable> void print(T obj) {
    System.out.println("[" + obj.getIdentity() + "] " + obj.getName());
}