Skip to Content
Kotlin 编程实践
book

Kotlin 编程实践

by Ken Kousen
July 2020
Intermediate to advanced
235 pages
3h 37m
Chinese
China Machine Press
Content preview from Kotlin 编程实践
40
2
不错,但是请记住,当你调用可选参数的构造函数时,你并没有在超类中调用类似的构
造函数。所有调用都通过拥有最多参数的单个构造函数进行。
调用被 @JvmOverloads 标记的构造函数时,不会调用具有相同数量参数
super。相反,他们使用提供的默认值调用完整的构造函数。
Java 中,每个构造函数使用 super 关键字调用其父构造函数,当你重载构造函数时,
你经常使用相同数量的参数调用 super。但这在本例中不会发生。被调用的超类构造函
数拥有所有参数,并提供了默认值。
2.4
显式类型转换
问题
Kotlin 不会自动将原生类型转换到一个位数更大的原生类型,例如 Int 转型到 Long
解决方案
使用 toInttoLong 等显式转换函数来显式转换位数较小的类型。
讨论
Kotlin Java 开发者提供的一个惊喜就是,它不会自动将一个位数较短的类型转换到一
个位数较长的类型。例如,在 Java 中,编写示例 2-22 中的代码是完全正常的。
示例 2-22:在 Java 中将短位数的原始类型转换为长位数
int myInt = 3;
long myLong = myInt;
 自动将 int 转型为 long
自从 Java 1.5 中引入自动装箱之后,原生类型到包装类型的转换变得很容易,但是将一
个包装类型转换为另一个包装类型仍然需要额外的代码,参见示例 2-23
示例 2-23:将 Integer 转型为 Long
Integer myInteger = 3; ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

GraphQL 学习指南

GraphQL 学习指南

Eve Porcello, Alex Banks
Java 技术手册(原书第7版)

Java 技术手册(原书第7版)

Benjamin J. Evans, David Flanagan
Python机器学习基础教程

Python机器学习基础教程

Andreas C. Müller, Sarah Guido

Publisher Resources

ISBN: 9787111659624