在Kotlin中,与这个表达式等价的是什么?

a ? b : c

这在Kotlin中是无效的代码。


当前回答

您可以在Kotlin中使用if表达式。在Kotlin中if是一个带有结果值的表达式。所以在Kotlin中,我们可以写

fun max(a: Int, b: Int) = if (a > b) a else b

在Java中,我们可以用更大的代码实现同样的效果

int max(int a, int b) {
return a > b ? a : b
}

其他回答

在kotlin中没有三元运算符,因为if else块返回值。

所以,你可以: Val Max = if (a > b) a else b 而不是java的Max = (a > b) ?B: c

我们也可以使用when construction,它也返回值:

val max = when(a > b) {
    true -> a
    false -> b
}

下面是kotlin文档的链接:Control Flow: if, when, for, while

使用以下中缀函数,我可以覆盖许多常见的用例,几乎与在Python中实现的方式相同:

class TestKotlinTernaryConditionalOperator {

    @Test
    fun testAndOrInfixFunctions() {
        Assertions.assertThat(true and "yes" or "no").isEqualTo("yes")
        Assertions.assertThat(false and "yes" or "no").isEqualTo("no")

        Assertions.assertThat("A" and "yes" or "no").isEqualTo("yes")
        Assertions.assertThat("" and "yes" or "no").isEqualTo("no")

        Assertions.assertThat(1 and "yes" or "no").isEqualTo("yes")
        Assertions.assertThat(0 and "yes" or "no").isEqualTo("no")

        Assertions.assertThat(Date() and "yes" or "no").isEqualTo("yes")
        @Suppress("CAST_NEVER_SUCCEEDS")
        Assertions.assertThat(null as Date? and "yes" or "no").isEqualTo("no")
    }
}

infix fun <E> Boolean?.and(other: E?): E? = if (this == true) other else null
infix fun <E> CharSequence?.and(other: E?): E? = if (!(this ?: "").isEmpty()) other else null
infix fun <E> Number?.and(other: E?): E? = if (this?.toInt() ?: 0 != 0) other else null
infix fun <E> Any?.and(other: E?): E? = if (this != null) other else null
infix fun <E> E?.or(other: E?): E? = this ?: other

在Java中,if是语句,但在Kotlin中if是表达式。它被称为表达式,因为它比较a和b的值并返回最大值。因此,在Kotlin中没有三元运算符(a>b)?A:b,因为它被if表达式所取代。

您可以定义自己的布尔扩展函数,当布尔值为false时返回null,以提供类似于三元运算符的结构:

infix fun <T> Boolean.then(param: T): T? = if (this) param else null

这是a ?B: c表达式翻译成a然后B ?: c,像这样:

println(condition then "yes" ?: "no")

更新: 但是要做一些更类似于java的条件切换,您将需要类似的东西

infix fun <T>布尔值。then(参数:()-> T): T?= if (this) param() else null

Println(条件然后{"yes"} ?: "no") 注意这个。它的内容计算应该推迟,直到我们确定条件为真

这个看起来很笨拙,这就是为什么有很高的要求存在的Java三元运算符移植到Kotlin

在Kotlin中没有三元运算符。乍一看,这似乎有问题。但是我们可以用内联if else语句因为这是表达式。我们要做的就是

var number = if(n>0) "Positive" else "Negetive"

这里我们可以用else if来阻塞我们需要的太多。像- - - - - -

var number = if(n>0) "Positive" else if(n<0) "Negative" else "Zero"

这一行比三元运算符简单易读。当我们在Java中使用多个三元运算符时,这看起来很可怕。但是这里我们有一个清晰的语法。甚至我们也可以把它写成多行。