kotlin中is、!is、as、as?的使用方式

1.is运算符相当于java中的instanceof,用来判断当前类型是不是对应的类型(可以是对应的类型的子类),说的很抽象,上代码看一下。

fun main() {
    val str = "helloWorld"
    println(str is String)
}

输出:true

2.!isis的否定形式(就是效果跟is相反),上代码看一下

fun main() {
    val x = 10
    println(x !is Int)
}

输出:false

3.as是相当于是显示类型转换(就是强制类型转换),转换成功就转换成功,转换失败会报错

4.as?as安全一点,转换成功就转换成功,转换失败会返回null

注意:父类转换为子类是对OOP(Object Oriented Programming,面向对象编程)的严重违反,不推荐使用。(父类是不能转换为子类的,子类包含了父类所有的方法和属性,而父类不一定包含子类所有的方法和属性,所以这种转换是不可以的,即便是两个具有父子关系的空类型,也不行。)

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Chapter 1, The Power of Kotlin, is the introductory chapter. It covers why Kotlin is quickly becoming a force to reckon with. Kotlin positions itself as the smart choice to the various stakeholders, such as the programmer, the manager, and the businesses. The chapter looks at the technology and business reasons that fuel the adoption of Kotlin. Chapter 2, Geospatial Messenger – Spring Boot, covers the use of Spring Boot technology to create robust server-side applications. Spring is one of the most well-known and well-respected server-side frameworks in the Java space, and Spring Boot is its less verbose and more powerful version. This chapter covers how a Geospatial Messenger application is created with Spring Boot technology using the Kotlin language. Chapter 3, Social Media Aggregator Android App, is using Kotlin to create native Android apps. With Google officially recognizing Kotlin's use to develop Android apps, this is the most widespread application to use the Kotlin language. Today's mobile apps hardly work in isolation. They work with a server. This common scenario is illustrated with a social media aggregator app. Chapter 4, Weather App Using Kotlin for JavaScript, explores the use of Kotlin to create rich internet apps that work within the browser. JavaScript is clearly the de facto standard language that works across all the browsers. However, Kotlin is clearly superior to JavaScript in many ways. This chapter shows how one can have the best of both worlds by doing the coding in Kotlin and, then, transpiling the code to JavaScript so that it works with the browsers seamlessly. The example that we will build is a simple weather application. Chapter 5, Chat Application with Server-Side JavaScript Generation, explores how the same facility of transpiling Kotlin to Javascript can be used on the server side. JavaScript is used on the server side within the popular and fast Node.js framework. Kotlin can be used to create Node.js applications, as the JavaScript code that runs with Node.js can be generated from it. This chapter shows how to do so by creating a simple chat application. Chapter 6, News Feed – REST API, covers the use of Kotlin specifically to create REST services. We will develop a News Feed application with the Ktor framework, which is a leading Kotlin-only framework used for server-side applications. Chapter 7, CSV Reader in Kotlin Native, explores a bleeding edge technology—Kotlin Native—which is about compiling Kotlin code directly to platform-specific executables. Although not mature, Kotlin Native is worth watching as it quickly marches to its promise of becoming the only language to create native applications across disparate platforms such as iOS and Raspberry Pi. In this chapter, a small CSV reader utility is built with Kotlin/Native. Chapter 8, Dictionary Desktop Application - Tornado FX, is about using Kotlin to create a cross-platform desktop application based on Java technology. Tornado FX is a Kotlin-specific framework, which is based on the most advanced Java GUI framework, that is, Java FX. This chapter illustrates the power of Tornado FX with a dictionary application.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心脏dance

如果解决了您的疑惑,谢谢打赏呦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值