首页 > 其他 > 详细

[Kotlin] Typecheck with 'is' keyword

时间:2020-10-26 19:43:58      阅读:18      评论:0      收藏:0      [点我收藏+]
val randomNumber = Random().nextInt(3)

if (randomNumber is BigDecimal) {
    result = result.add(BigDecimal(36))
}

If you use type check, then ‘result‘ is auto casting to BigDecimal type.

 

‘as‘ keyword

val randomNumber = Random().nextInt(3)

if (randomNumber is BigDecimal) {
    result = result.add(BigDecimal(36))
} else {
    val tempResult: String = result as String
    result = tempResult.toUpperCase()
}

 

[Kotlin] Typecheck with 'is' keyword

原文:https://www.cnblogs.com/Answer1215/p/13880068.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!