Kotlin:方法引用不起作用? [英] Kotlin: Method reference not working?
问题描述
似乎我无法在Kotlin中使用对象的方法引用. Java中存在此功能.
It seems I'm unable to use a method reference of an object in Kotlin. This feature exists in Java.
例如,在Java中,如果我遍历字符串以将每个字符附加到编写器:
For example in Java if I was looping through a string to append each character to a writer:
string.forEach(writer::append);
但是在Kotlin中,使用相同的语法不起作用是因为:
But in Kotlin using the same syntax does not work because:
推荐答案
Kotlin目前仅支持对顶级和本地函数以及 classs 成员的引用,而不支持单个实例.请在此处参阅文档.
For now, Kotlin only supports references to top-level and local functions and members of classes, not individual instances. See the docs here.
因此,您可以说Writer::append
并获得函数Writer.(Char) -> Writer
,但是目前不支持采用writer
实例并说writer::append
以获得函数(Char) -> Writer
.
So, you can say Writer::append
and get a function Writer.(Char) -> Writer
, but taking a writer
instance and saying writer::append
to get a function (Char) -> Writer
is not supported at the moment.
这篇关于Kotlin:方法引用不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!