是否有可能在java中引用嵌套的泛型参数? [英] Is it possible to reference a nested generic parameter in java?

查看:189
本文介绍了是否有可能在java中引用嵌套的泛型参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定这是什么技术术语,但考虑一个接口:

  public interface SomeInterface< T> ; {
public T doSomething();
}

然后是第二个界面:

  public interface SomeRelatedInterface< T,D extends SomeInterface< T>> {
public T doSomethingRelated(D relative);
}

是否可以将第二个接口制作成仅需要一个通用参数,然后让doSomethingRelated方法隐式地在其声明中提取返回类型。这是不合法的,但这是我想知道是否可以用其他方式完成:

  public interface< T> ; SomeRelatedInterface< D extends SomeInterface< T>> {
public T doSomethingRelated(D relative);
}

编辑(发布赏金):在这一点上我正在寻找在这个问题上是语言需要这种重复的原因。

公共接口SomeRelatedInterface< T> {
T doSomethingRelated(SomeInterface< T> relative);
}


I'm not sure what the technical term for this is, but consider an interface:

public interface SomeInterface<T> {
     public T doSomething();
}

And then a second interface:

public interface SomeRelatedInterface<T, D extends SomeInterface<T>> {
     public T doSomethingRelated(D relative);
}

Is it possible to craft the second interface to only require one generic parameter, and then have the doSomethingRelated method implicitly extract the return type in its declaration. This is not legal, but this is what I am wondering if can be done in some other means:

public interface <T> SomeRelatedInterface<D extends SomeInterface<T>> {
     public T doSomethingRelated(D relative);
}

EDIT (On posting the bounty): At this point what I am looking for on this question is the reason that the language requires this duplication. That is what has been missing from the answers until now to get one accepted.

解决方案

public interface SomeRelatedInterface<T> {  
    T doSomethingRelated(SomeInterface<T> relative);
}

这篇关于是否有可能在java中引用嵌套的泛型参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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