Kotlin教程:Elvis运算符(?:)

2020-04-2121:47:14编程语言入门到精通Comments1,786 views字数 2250阅读模式

Elvis运算符(?:)用于返回非null值,即使条件表达式为null。 它还用于检查值的空安全性。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18278.html

在某些情况下,可以声明一个保存空引用的变量。 假设一个包含空引用的变量str,在程序中使用str之前将检查它的可空性。 如果发现变量str不为null,则其属性将使用,否则使用其他非空值。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18278.html

var str: String? = null   
var str2: String? = "May be declare nullable string"

在上面的代码中,str包含一个null值,在访问str的值之前需要执行安全检查,字符串是否包含值。 在传统方法中,使用if...else语句执行此安全检查。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18278.html

var len1: Int = if (str != null) str.length else -1  
var len2:  Int = if (str2 != null) str.length else -1

示例代码 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18278.html

fun main(args: Array<String>){

    var str: String? = null
    var str2: String? = "May be declare nullable string"
    var len1:  Int = if (str != null) str.length else -1
    var len2:  Int = if (str2 != null) str2.length else -1
    println("Length of str is ${len1}")
    println("Length of str2 is ${len2}")
}
Kotlin

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18278.html

Length of str is -1
Length of str2 is 30
Shell

Kotlin提供称为Elvis运算符(?:)的高级运算符,即使条件表达式为空,也返回非空值。 以上if...else运算符可以使用Elvis运算符表示如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18278.html

var len1:  Int = str?.length ?: -1  
var len2:  Int = str2?.length ?:  -1
Kotlin

Elvis运算符将表达式返回?: -1。 (str ?.length)如果它不为null,否则它将表达式返回?:, 即-1。 仅当左侧返回null时,才会评估Elvis运算符的右侧表达式。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18278.html

Kotlin Elvis运算符示例文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18278.html

fun main(args: Array<String>){

    var str: String? = null
    var str2: String? = "May be declare nullable string"
    var len1:  Int = str ?.length ?: -1
    var len2:  Int = str2 ?.length ?:  -1

    println("Length of str is ${len1}")
    println("Length of str2 is ${len2}")
}
Kotlin

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18278.html

Length of str is -1
Length of str2 is 30
Shell

当Kotlin抛出并返回一个表达式时,它们也可以在Elvis运算符的右侧使用。 这可以用于检查函数参数:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18278.html

funfunctionName(node: Node): String? {
    val parent = node.getParent() ?: return null
    val name = node.getName() ?: throw IllegalArgumentException("name expected")
    // ...  
}
Kotlin

Kotlin Elvis运算符使用throw和return表达式文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18278.html

fun main(args: Array<String>){
    val fruitName: String = fruits()
    println(fruitName)
}
fun fruits(): String{
    val str: String? ="abc"
    val strLength: Int = if(str!= null) str.length else -1
    val strLength2: Int = str?.length ?: -1
    var string = "str = $str\n"+
            "strLength = $strLength\n"+
            "strLength2 = $strLength2\n\n"

    fun check(textOne: String?, textTwo: String?): String?{
        val textOne = textOne ?: return null
        val textTwo = textTwo ?: IllegalArgumentException("text exception")

        return "\ntextOne = $textOne\n"+
                "textTwo = $textTwo\n"
    }
    string += "check(null,\"mango\") = ${check(null,"mango")}\n" +
            "check(\"apple\",\"orange\") = ${check("apple","orange")}\n"
    return string
}
Kotlin

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18278.html

str = abc
strLength = 3
strLength2 = 3

check(null,"mango") = null
check("apple","orange") = 
textOne = apple
textTwo = orange

//原文出自【易百教程】,商业转载请联系作者获得授权,非商业转载请保留原文链接:https://www.yiibai.com/kotlin/kotlin-elvis-operator.html#article-start文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18278.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18278.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/18278.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定