Skip to Content
Java 到 Kotlin
book

Java 到 Kotlin

by Duncan McGregor, Nat Pryce
May 2025
Intermediate to advanced
424 pages
6h 12m
Chinese
O'Reilly Media, Inc.
Content preview from Java 到 Kotlin

第 10 章 从函数到扩展函数 从函数到扩展函数

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

Kotlin 有一种特殊的过程,叫做扩展函数,它的调用方式类似于方法,但实际上(通常)是一个顶级函数。 从普通函数转换为扩展函数,再转换回来,非常简单。 什么时候我们应该选择其中一种呢?

功能和方法

面向对象编程 是一门通过向对象发送消息来解决问题的艺术。 想知道myString 的长度吗?通过向它发送消息myString.length() 来询问它。 想将该字符串打印到控制台吗?将该字符串放入消息中,然后让另一个代表控制台的对象为您打印: System.out.println(myString)在经典的 OO 语言中,我们通过定义类上的方法来定义对象如何对消息做出反应。 方法与类绑定,可以访问与特定实例相关的成员(字段和其他方法)。 当我们调用一个方法时,运行时会安排调用正确的版本(取决于对象的运行时类型),并安排它访问实例状态。

相反,在函数式编程中,我们通过调用带有值的函数来解决问题。我们通过将myString 的长度传递给一个函数来找到它:length(myString) 。我们通过println(myString) 打印到控制台,如果我们想打印到其他地方,我们会将它传递给函数:println(myString, System.err) 。函数不是根据类型定义的,函数参数和结果都有类型。

范式各有利弊,但现在我们只考虑可发现性和可扩展性。

这里是Customer 类型:

data class Customer(
    val id: String,
    val givenName: String,
    val familyName: String
) {
    ...
}

这是一个类,所以我们马上就知道可以发送信息询问id,givenName, 和familyName 。那么其他操作呢? 在基于类的系统中,我们只需向下滚动就可以看到我们可以发送的其他信息:

data class Customer(
    val id: String,
    val givenName: String,
    val familyName: String
) {
    val fullName get() = "$givenName $familyName"
}

通常,我们甚至不需要查看定义。如果我们有一个变量val customer: Customer ,我们可以键入customer. ,集成开发环境就会迫不及待地告诉我们可以调用idgivenNamefamilyNamefullName 。事实上,这种自动完成在很多方面都比查看类的定义更好,因为它还向我们展示了在超类中定义的或隐含在语言中的其他操作(equalscopy 等)。

在 功能分解中,fullName 将是一个函数,如果我们怀疑它存在,就必须在代码库中搜索它。在这种情况下,它将是一个唯一参数类型为Customer 的函数。要让 IntelliJ 帮助我们是出乎意料地困难。按参数类型分组的 "查找用法"(Find Usages)可以完成这项工作,但它并不方便。 在实践中,我们希望在源代码中找到Customer 的定义及其基本操作,也许是在同一个文件中,或者至少是在命名空间中,因此我们可能会导航到那里,并在我们期望的地方找到函数,但我们的工具并没有帮上什么忙。

在可发现性方面,OO 得了一分。 那么可扩展性呢?如果我们想在Customer 上添加一个操作,会发生什么情况呢?市场营销部门希望在某个报告或其他报告中将 ...

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

使用 Kotlin 进行 Android 编程

使用 Kotlin 进行 Android 编程

Pierre-Olivier Laurence, Amanda Hinchman-Dominguez, G. Blake Meike, Mike Dunn
Programming with MicroPython

Programming with MicroPython

Nicholas H. Tollervey

Publisher Resources

ISBN: 9798341659209