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:
是否可以将第二个接口设计为只需要一个泛型参数,然后让doSomethingRelated方法在其声明中隐式提取返回类型。这不合法,但我想知道是否可以通过其他方式完成:
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.
编辑(在发布赏金时):此时我在这个问题上寻找的是语言需要这种复制的原因。到目前为止,答案中已经缺少这一点,以便接受一个。
5 个解决方案
#1
public interface SomeRelatedInterface<T> {
T doSomethingRelated(SomeInterface<T> relative);
}
#2
"At this point what I am looking for on this question is the reason that the language requires this duplication"
“在这一点上,我在这个问题上寻找的是语言需要这种重复的原因”
Well, the language requires that you define 2 type parameters in your example because there are, um, 2 type parameters in the problem you describe: you wish a method to be variable in both the type T
and also in the implementation of SomeInterface
.
好吧,该语言要求您在示例中定义2个类型参数,因为在您描述的问题中有两个类型参数:您希望方法在类型T和SomeInterface的实现中都是可变的。
These are orthogonal considerations and hence you need more than one type parameter to represent them.
这些是正交考虑因素,因此您需要多个类型参数来表示它们。
Type parameters do not of course need to be defined on a class/interface; they can be defined on a method. J-16 SDiZ's answer allows your related class/interface to have one type parameter only. The second type parameter is then declared only where it is needed, on the doSomethingRelated
method
类型参数当然不需要在类/接口上定义;它们可以在方法上定义。 J-16 SDiZ的答案允许您的相关类/接口只有一个类型参数。然后,在doSomethingRelated方法上,仅在需要时声明第二个类型参数
#3
Well, I started a bounty on this question, and didn't know that SO's behavior was to award someone the answer (congratulations Daniel), I thought the rep would go unrewarded and I would lose it. Oh well.
好吧,我开始对这个问题给予赏金,并且不知道SO的行为是给某人一个答案(祝贺丹尼尔),我认为代表会没有报酬,我会失去它。那好吧。
Anyway, I finally have my answer. From here:
无论如何,我终于得到了答案。从这里:
Unfortunately, for the purposes of backwards compatibility, new Map() indicates a raw type, and therefore cannot be used for type inference.
不幸的是,出于向后兼容的目的,新的Map()表示原始类型,因此不能用于类型推断。
So basically when creating a class and passing in the type parameter, type inference was disabled to leave room for the raw type. So in my case there could be some type inference but that would be a question of having a more complex different kind of type inference to handle this case, which wasn't done.
因此,基本上在创建类并传入type参数时,类型推断被禁用以为原始类型留出空间。所以在我的情况下可能会有一些类型推断,但这将是一个问题,有一个更复杂的不同类型的推断来处理这种情况,这是没有完成的。
#4
See if this suit your need:
看看这是否适合您的需要:
public interface SomeRelatedInterface<T> {
public <D extends SomeInterface<T>> T doSomethingRelated(D relative);
}
#5
As the type parameters are erased at compile time, IMHO you unfortunately cannot achieve what you want without specifying T as the second type parameter, just as you did in your first example.
由于类型参数在编译时被擦除,恕我直言,遗憾的是,如果不将T指定为第二个类型参数,就无法实现您想要的效果,就像您在第一个示例中所做的那样。
#1
public interface SomeRelatedInterface<T> {
T doSomethingRelated(SomeInterface<T> relative);
}
#2
"At this point what I am looking for on this question is the reason that the language requires this duplication"
“在这一点上,我在这个问题上寻找的是语言需要这种重复的原因”
Well, the language requires that you define 2 type parameters in your example because there are, um, 2 type parameters in the problem you describe: you wish a method to be variable in both the type T
and also in the implementation of SomeInterface
.
好吧,该语言要求您在示例中定义2个类型参数,因为在您描述的问题中有两个类型参数:您希望方法在类型T和SomeInterface的实现中都是可变的。
These are orthogonal considerations and hence you need more than one type parameter to represent them.
这些是正交考虑因素,因此您需要多个类型参数来表示它们。
Type parameters do not of course need to be defined on a class/interface; they can be defined on a method. J-16 SDiZ's answer allows your related class/interface to have one type parameter only. The second type parameter is then declared only where it is needed, on the doSomethingRelated
method
类型参数当然不需要在类/接口上定义;它们可以在方法上定义。 J-16 SDiZ的答案允许您的相关类/接口只有一个类型参数。然后,在doSomethingRelated方法上,仅在需要时声明第二个类型参数
#3
Well, I started a bounty on this question, and didn't know that SO's behavior was to award someone the answer (congratulations Daniel), I thought the rep would go unrewarded and I would lose it. Oh well.
好吧,我开始对这个问题给予赏金,并且不知道SO的行为是给某人一个答案(祝贺丹尼尔),我认为代表会没有报酬,我会失去它。那好吧。
Anyway, I finally have my answer. From here:
无论如何,我终于得到了答案。从这里:
Unfortunately, for the purposes of backwards compatibility, new Map() indicates a raw type, and therefore cannot be used for type inference.
不幸的是,出于向后兼容的目的,新的Map()表示原始类型,因此不能用于类型推断。
So basically when creating a class and passing in the type parameter, type inference was disabled to leave room for the raw type. So in my case there could be some type inference but that would be a question of having a more complex different kind of type inference to handle this case, which wasn't done.
因此,基本上在创建类并传入type参数时,类型推断被禁用以为原始类型留出空间。所以在我的情况下可能会有一些类型推断,但这将是一个问题,有一个更复杂的不同类型的推断来处理这种情况,这是没有完成的。
#4
See if this suit your need:
看看这是否适合您的需要:
public interface SomeRelatedInterface<T> {
public <D extends SomeInterface<T>> T doSomethingRelated(D relative);
}
#5
As the type parameters are erased at compile time, IMHO you unfortunately cannot achieve what you want without specifying T as the second type parameter, just as you did in your first example.
由于类型参数在编译时被擦除,恕我直言,遗憾的是,如果不将T指定为第二个类型参数,就无法实现您想要的效果,就像您在第一个示例中所做的那样。