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

第 22 章 从类到函数 从类到函数

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

面向对象程序员擅长通过创建类型来解决问题。 函数式程序员倾向于用函数来增强现有类型。 不定义新类型,我们还能走多远?

第 15 章 "从封装集合到类型别名"中,我们看到了使用原始集合的优势;在第 16 章 "从函数接口到函数"中,我们了解了如何使用内置函数类型而不是创建新类型。 在本章中,我们将运用所学到的知识,从头开始编写一些 Kotlin 代码。

即使在使用 REST API 和 Webhooks 的今天,许多企业间的自动通信也是通过 安全文件传输协议(SFTP)以表格文本数据的形式交换的。Travelator 需要导入露营地位置、兴趣点、未结算账单等数据,所有数据都以常规行和列的形式存在,列之间有不同的分隔符,其余行的列也有标题命名和无标题命名之分。 在第 20 章中,我们 看到一个团队创建了自己的解析器;而在其他地方,我们使用的是久经考验、值得信赖的Apache Commons CSV 库。老实说,对于大多数用途,我们还是会使用 Commons CSV,因为它开箱即用,可以很好地配置特殊情况,而且与 Kotlin 配合得非常好。

今天,我们将看看一个干净的 Kotlin 解析器会是什么样子。 完成后,我们将把自己的成果与 Commons 的 CSV 功能进行比较,从而了解 Java 和 Kotlin 的颗粒是如何导致不同的 API 和实现的。

验收测试

你可能已经从前面的章节中了解到,Travelator 的开发人员都是极限程序员(《极限编程详解:拥抱变化》)。我们首先编写代码测试,从高级验收测试开始。我们正在开发一个表格阅读器,因此我们创建了一个带有存根方法的类TableReaderAcceptanceTests ,并检查它是否运行:

class TableReaderAcceptanceTests {
    @Test
    fun test() {
    }
}

它确实运行了(甚至还通过了!),所以现在我们可以开始正式编码了。

验收测试的部分工作就是帮助我们决定界面应该是什么样的。 在解析过一些文件后,我们知道我们几乎总是希望读取一个文件,然后返回某个域类型的值列表,每行(非标题)一个。 让我们以Measurement 作为域类型,勾画出测试草图:

class TableReaderAcceptanceTests {
    data class Measurement(
        val t: Double,
        val x: Double,
        val y: Double,
    )

    @Test
    fun `acceptance test`() {
        val input = listOf(
            "time,x,y",
            "0.0,  1,  1",
            "0.1,1.1,1.2",
            "0.2,1.2,1.4",
        )
        val expected = listOf(
            Measurement(0.0, 1.0, 1.0),
            Measurement(0.1, 1.1, 1.2),
            Measurement(0.2, 1.2, 1.4)
        )
        assertEquals(
            expected,
            someFunction(input)
        )
    }

    private 
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