Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kotlin 我可以在没有'this'的情况下在自己的类中应用中缀函数吗?_Kotlin - Fatal编程技术网

Kotlin 我可以在没有'this'的情况下在自己的类中应用中缀函数吗?

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

在科特林,我们有中缀

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) {}
}
但对于中缀,下面的内容是不好的

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

接收器函数名参数

无法避免。

相关:可能重复