Kotlin 我可以在没有'this'的情况下在自己的类中应用中缀函数吗?
在科特林,我们有中缀 e、 当我们有Kotlin 我可以在没有'this'的情况下在自己的类中应用中缀函数吗?,kotlin,Kotlin,在科特林,我们有中缀 e、 当我们有 fun Int.test(value: Int) {} 我们可以使用 1.test(2) 当我们放中缀的时候 infix fun Int.test(value: Int) {} 我们可以用 1 test 2 对于一个班级来说,下面的内容是可以的 class myclass { fun main() { test(1) } fun test(value: Int) {} } 但对于中缀,下面的内容是不好的 cl
fun Int.test(value: Int) {}
我们可以使用
1.test(2)
当我们放中缀的时候
infix fun Int.test(value: Int) {}
我们可以用
1 test 2
对于一个班级来说,下面的内容是可以的
class myclass {
fun main() {
test(1)
}
fun test(value: Int) {}
}
但对于中缀,下面的内容是不好的
class myclass {
fun main() {
test 1
}
infix fun test(value: Int) {}
}
显然,它必须有
class myclass {
fun main() {
this test 1
}
infix fun test(value: Int) {}
}
我可以省略
this
,因为test
是在类本身中调用的吗?它不能省略,在使用中缀
函数时,总是需要一个左操作数,在您的情况下就是this
:
“接收器函数名参数
”
无法避免。相关:可能重复