kev*_*mes 11 java generics method-reference
我在"Java - 初学者指南"中阅读了以下代码
interface SomeTest <T>
{
boolean test(T n, T m);
}
class MyClass
{
static <T> boolean myGenMeth(T x, T y)
{
boolean result = false;
// ...
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
以下声明有效
SomeTest <Integer> mRef = MyClass :: <Integer> myGenMeth;
Run Code Online (Sandbox Code Playgroud)
关于上述代码的解释有两点
1 - 当泛型方法被指定为方法引用时,其类型参数
::位于方法名称之前和之后.2 - 如果指定了泛型类,则type参数在类名后面,并在其前面
::.
上面的代码是第一个引用点的示例
有人能为我提供一个实现第二个引用点的代码示例吗?
(基本上我不明白第二个引用点).
第二个引号仅表示类型参数属于该类。例如:
class MyClass<T>
{
public boolean myGenMeth(T x, T y)
{
boolean result = false;
// ...
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
这将被称为这样:
SomeTest<Integer> mRef = new MyClass<Integer>() :: myGenMeth;
Run Code Online (Sandbox Code Playgroud)