在Java中,有没有办法指定参数实现两个接口 [英] In Java, is there a way to specify that an argument implements two interfaces

查看:118
本文介绍了在Java中,有没有办法指定参数实现两个接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很想做这样的代码,使用jGraphT

I am tempted to do such kind of code, using jGraphT

/*
  interface DirectedGraph<V,E> { ...}
  interface WeightedGraph<V,E> { ...}
*/

public class SteinerTreeCalc  {

    public SteinerTreeCalc( < ??? implements DirectedGraph<V,E>, WeightedGraph<V,E> > graph )  {
     ......
    }


}

我想创建一个构造函数,要求实现两个接口的对象。

I want to create a constructor that ask for an object implementing two interfaces.

更新:

在我的目标中,已经为Vertex和Edges(V和E)选择了类,但非常感谢那些想出的人:

In my goal, there are already chosen classes for Vertex and Edges (V and E), but thanks a lot to people who come up with :

public class SteinerTreeCalc <V, E, T extends DirectedGraph<V, E> & WeightedGraph<V, E>>  
{ 
   ....
}


推荐答案

是的,有可能:

public class SteinerTreeCalc<T extends DirectedGraph<V,E> & WeightedGraph<V,E>> {
  public SteinerTreeCalc(T graph) {
    ......
  }
}

这篇关于在Java中,有没有办法指定参数实现两个接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆