用于指定泛型方法的方法引用的语法 [英] Syntax for specifying a method reference to a generic method
问题描述
我在Java - 初学者指南中阅读了以下代码:
interface SomeTest< T>
{
布尔测试(T n,T m);
}
class MyClass
{
static< T> boolean myGenMeth(T x,T y)
{
布尔结果= false;
// ...
返回结果;
以下语句有效
SomeTest< Integer> mRef = MyClass ::< Integer> myGenMeth;
关于上述代码的解释有两点:
1 - 将泛型方法指定为方法引用时,其类型参数位于
::
之后,方法之前名称。
$ b 2 - 如果指定了一个泛型类,则type参数紧跟类名,并位于:: $之前c $ c $。
我的查询: -
是第一个引用点的例子
有人可以提供一个实现第二个引用点的代码示例吗?
(基本上我不明白第二个引用的要点)。 解决方案
第二个引用的点只是表示类型参数属于该类。例如:
class MyClass< T>
{
public boolean myGenMeth(T x,T y)
{
布尔结果= false;
// ...
返回结果;
然后这样调用:
SomeTest< Integer> mRef = new MyClass< Integer>():: myGenMeth;
I read the following code in "Java - The beginner's guide"
interface SomeTest <T>
{
boolean test(T n, T m);
}
class MyClass
{
static <T> boolean myGenMeth(T x, T y)
{
boolean result = false;
// ...
return result;
}
}
The following statement is valid
SomeTest <Integer> mRef = MyClass :: <Integer> myGenMeth;
Two points were made regarding the explanation of the above code
1 - When a generic method is specified as a method reference, its type argument comes after the
::
and before the method name.2 - In case in which a generic class is specified, the type argument follows the class name and precedes the
::
.
My query:-
The code above is the example of the first quoted point
Can someone provide me an example of code which implement the second quoted point?
(Basically I don't understand the second quoted point).
The second quoted point just means that the type parameter belongs to the class. For example:
class MyClass<T>
{
public boolean myGenMeth(T x, T y)
{
boolean result = false;
// ...
return result;
}
}
This would then be called like this:
SomeTest<Integer> mRef = new MyClass<Integer>() :: myGenMeth;
这篇关于用于指定泛型方法的方法引用的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!