官术网_书友最值得收藏!

  • Functional Kotlin
  • Mario Arias Rivu Chakraborty
  • 55字
  • 2021-06-24 19:15:23

Checking for null

Check for null as a condition in the if block:

fun main(args: Array<String>) {
val nullableCupcake: Cupcake? = Cupcake.almond()

if (nullableCupcake != null) {
nullableCupcake.eat()
}
}

Kotlin will do a smart cast. Inside the if block, nullableCupcake is a Cupcake, not a Cupcake?; so, any method or property can be accessed.

主站蜘蛛池模板: 嘉黎县| 鹰潭市| 阿拉尔市| 北海市| 武穴市| 武邑县| 两当县| 乌兰察布市| 嘉义市| 诸暨市| 内黄县| 镇平县| 青岛市| 双鸭山市| 广州市| 潢川县| 林口县| 江都市| 昭苏县| 收藏| 民勤县| 阳江市| 华池县| 郎溪县| 江山市| 拉萨市| 古丈县| 湟源县| 承德县| 蒙阴县| 新化县| 遵义县| 大丰市| 石渠县| 泸定县| 蓬溪县| 景东| 吉木乃县| 谢通门县| 澎湖县| 潼关县|