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

第 11 章 从方法到属性 属性方法

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

Java 不区分属性访问方法和其他类型。 而 Kotlin 则将属性与成员函数区别对待。 什么时候我们应该优先选择计算属性,而不是返回结果的函数呢?

字段、访问器和属性

大多数编程语言 都允许我们以某种方式将数据组合在一起,并为组合属性赋予名称(通常还有类型)。

例如,这里是 ALGOL W 中的一条记录,由三个字段组成,ALGOL W 是最早支持记录类型的通用语言之一。(ALGOL W 也是 Tony Hoare 引入空引用的语言)。

RECORD PERSON (
    STRING(20) NAME;
    INTEGER AGE;
    LOGICAL MALE;
);

当时的情况与现在不同:真正的程序员只使用大写字母,性别是布尔值。

在 ALGOL W 中,我们可以(好吧,可以)更新PERSON 记录中的年龄:

AGE(WILMA) := AGE(WILMA) + 1;

在这种情况下,编译器将发出指令,进入记录的内存,找到代表威尔玛年龄的字节,并将其递增。 记录在其他语言中也称为 structs(结构体),是一种将相关数据分组的方便工具。 这里没有信息隐藏,只是组成而已。

大多数早期的面向对象系统(尤其是C++)都是基于这种记录机制的。 实例变量就是简单的记录字段,而方法(又称成员函数)则是持有函数指针的字段。 Smalltalk则不同。 Smalltalk对象可以拥有实例变量,但对这种状态的访问是通过向对象发送消息来实现的。 基本抽象是消息,而不是字段。

Java 的实现者在每种方法中都采取了一点。 对象可以有公共字段,但客户端不能直接进入其内存来检索这些字段;他们必须调用字节码指令来访问这些字段的值。 这使我们可以将类视为记录,同时允许运行时强制执行私有字段访问。

虽然允许直接访问字段,但从一开始就不鼓励这样做。 如果客户可以直接访问字段,我们就无法改变数据的内部表示,至少在不改变客户的情况下是这样。 如果客户可以直接更改字段,我们也无法维护字段之间的任何不变关系,正如我们在第 5 章中所看到的,在那个时代,对象就是关于更改的。 字段访问也不是多态的,因此子类无法改变它们的实现。 在那个时代,对象也是关于子类的。

因此, ,在 Java 中,我们通常编写访问器方法:getters 和 setters(如果需要),而不是直接访问字段。 Getters 除了返回字段的值外,通常什么也不做,但也可能从其他字段中计算出一个值。 Setters 可以维护不变式或触发事件,也可以更新一个字段或多个字段。

但有时,数据就是数据。 如果是这样,那么直接访问公共字段就可以了,特别是当我们有不可变的值(也就是不可变类型的最终字段)时。 对于更复杂的模型,多态行为和/或从字段或计算中访问值的统一方法就变得有用了,访问器方法也就派上了用场。

Kotlin 的设计者们选择将决定权从我们手中夺走,只支持访问器方法。 该语言不支持直接访问字段。 Kotlin 会生成访问 Java 类公共字段的代码,但本身并不定义公共字段。@JvmField(他们这样做是为了鼓励我们使用访问器,这样我们就可以在不影响客户端的情况下更改表示。

为了进一步鼓励访问者( ),Kotlin 允许我们在单个属性声明中同时生成私有成员变量和访问者。

因此,在 Java 中,我们可以直接对一个字段进行访问:

public class PersonWithPublicFields ...
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