Java Generic:特定参数类型的静态方法不匹配功能接口
下面的第一个分配未编译,但我不确定为什么,静态方法的方法签名与功能方法签名匹配,尽管它不使用类型参数。第二行填充了罚款,即使它是相同的签名,除了类型的参数化。
这背后的原因是什么?
public class GenericSample<T> {
public static void staticLambdaMtd(Integer a) {
}
public static<X> void staticLambdaMtd2(X a) {
}
// Funca<T> fa1 = GenericSample::staticLambdaMtd;//does not compile !
Funca<T> fa = GenericSample::staticLambdaMtd2;//does compile !
}
interface Funca<A> {
public void funct(A a);
}
The first assignment below doesn't compile, but I am not sure why, the method signature of the static method matches the functional method signature albeit it is not using type arguments. The second line compiles fine even though it is the same signature except for the type parameterization.
What is the reasoning behind this?
public class GenericSample<T> {
public static void staticLambdaMtd(Integer a) {
}
public static<X> void staticLambdaMtd2(X a) {
}
// Funca<T> fa1 = GenericSample::staticLambdaMtd;//does not compile !
Funca<T> fa = GenericSample::staticLambdaMtd2;//does compile !
}
interface Funca<A> {
public void funct(A a);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在混合通用参数
t
和类型整数
。这是行不通的,因为t
和整数
是不同的类型。仿制药是不变的。这意味着您可以分配给
list&lt; person&gt;
仅具有相同通用参数的另一个列表,即person
(不是integer
,> CAT
或t
)。同样,我们也无法将funca&lt; gt;
分配给类型funca&lt; t&gt;
的变量。有关更多信息,请看一下 tutorial
>由Oracle提供的
funca&lt; t&gt;
- IE某种任意类型的函数,因为只有整数
类型类型与方法签名匹配,但不符合任何类型(例如String
) ,cat
,bigDecimal
)将在类genericsample
将得到实例化时定义。第二个语句编编译,因为它不会期望任何特定类型。
X
只是占位符,t
。 Expressiongenericsample :: staticlambdamtd2
应分类为SO-CALLED poly表达式 ,即,因为您不提供类型的编译器需要从分配上下文中推断出来。因此,将推断出Expression
genericsample :: staticlambdamtd2
是类型为funca&lt; t&gt;
,第二个语句将符合正常。下面显示的所有作业都是有效的:
注释 表达式
genericsample :: staticlambdamtd2
可以从 poly expression 转动到a “独立形式” 通过提供一个通用参数(实际上,按规范中提供的定义方法参考始终是poly expressions ,因此我使用了“我使用过”独立的“引号”标记仅表示分配上下文将被忽略)。而且我们如何打破它:
You are mixing a generic parameter
T
and a typeInteger
. That wouldn't work becauseT
andInteger
are different types.Generics are invariant. That means you can assign to a
List<Person>
only another list having the same generic parameter, i.e.Person
(notInteger
,Cat
orT
). Similarly, we can't assignFunca<Integer>
to a variable of typeFunca<T>
.For more information, have a look at this tutorial provided by Oracle.
A function created using a static method
staticLambdaMtd(Integer a)
is assignable only to a variable of typeFunca<Integer>
, but notFunca<T>
- i.e. function of some arbitrary type, because onlyInteger
type would match the method signature, but not any type (likeString
,Cat
,BigDecimal
) that would be defined while classGenericSample
will get instantiated.The second statement compiles fine, because it doesn't expect any specific type.
X
is only a placeholder, as well asT
. And expressionGenericSample::staticLambdaMtd2
should be classified as a so-called poly expression, i.e. because you're not providing a type compiler needs to infer it from the assignment context.Therefore, the expression
GenericSample::staticLambdaMtd2
would be inferred as being of typeFunca<T>
and the second statement will compile fine.All assignments shown below are valid:
Note that expression
GenericSample::staticLambdaMtd2
can be turned from a poly expression into a "standalone form" by providing a generic parameter (in fact, method references by definition provided in the specification are always poly expressions, therefore I've used "standalone" in quotation marks meaning only that assignment context will be ignored).And that we how can break it: