【发布时间】:2020-10-04 09:12:02
【问题描述】:
例如,这里是一个 Java 类
public class Thing {
...
public int thing;
public int getThing() { return thing; }
public void setThing(int t) { thing = t; }
}
在 Kotlin 中,如果我想访问 thing,我会执行以下操作:
val t = Thing()
t.thing // get
t.thing = 42 //set
在反编译的 Kotlin 字节码中,我看到的是 Kotlin 使用 getter 和 setter:
t.getThing()
t.setThing(42)
不知道有没有办法直接访问字段t.thing而不是使用getter和setter?
【问题讨论】:
-
当你真的不需要直接访问时考虑只使用 getter 和 setter(大多数情况下从不):stackoverflow.com/questions/1568091/…