我有一个用于 JSON 序列化的简单类。为此,外部接口使用String
s,但内部表示不同。
public class TheClass {
private final ComplexInfo info;
public TheClass(String info) {
this.info = new ComplexInfo(info);
}
public String getInfo() {
return this.info.getAsString();
}
// ...more stuff which uses the ComplexInfo...
}
我在 Kotlin 中有这个工作(不确定是否有更好的方法)。但是非 val/var 构造函数阻止我使用data
.
/*data*/ class TheClass(info: String) {
private val _info = ComplexInfo(info)
val info: String
get() = _info.getAsString()
// ...more stuff which uses the ComplexInfo...
}
我如何让这个工作作为一个data class
?