[英]How does plus sign refers to plus function in Kotlin and is it an infix function?
我有三個問題。
1、加號(+)或in運算符如何引用plus()和contains()函數?
2. 這些是中綴函數嗎?
他們沒有中綴符號。
3. 有什么方法可以將自定義字符定義為操作符?
運算符重載在這里定義
https://kotlinlang.org/docs/reference/operator-overloading.html
Expression Translated to
a + b a.plus(b)
a - b a.minus(b)
a * b a.times(b)
a / b a.div(b)
a % b a.rem(b), a.mod(b) (deprecated)
a..b a.rangeTo(b)
特殊符號和保留字在這里
https://kotlinlang.org/docs/reference/keyword-reference.html
運算符和特殊符號
Kotlin 支持以下運算符和特殊符號:
+, -, *, /, % - 數學運算符
1) & 2)
+
和in
(以及其他一些)是語言內置的,並且是隱式中綴並具有相關的運算符函數(加 & 包含)。
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/plus.html#kotlin $plus(kotlin.String,%20kotlin.Any)/other
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/contains.html
3) 是的,但你必須轉義像這樣的字符`$`
或`^`
infix fun Int.`√`(arg: Double): Double {
return Math.pow(arg, 1.0 / this.toDouble())
}
infix fun Double.`^^`(arg: Double): Double {
return Math.pow(arg, this)
}
fun main() {
println( 3 `√` 27.0 ) // 3.0
println( 3.0 `^^` 3.0 ) // 27.0
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.