[英]Data class inheriting from Java class
I have a data class in Kotlin that inherits from a Java class, which defines a constructor with 1 argument, 我在Kotlin中有一个数据类,该数据类继承自Java类,该类定义了带有1个参数的构造函数,
public BaseClass(String userSessionId) {
this.userSessionId = userSessionId;
}
My Kotlin class is defined as this 我的Kotlin班级定义为
class DerivedClass(
userSessionId: String,
var other: Other? = null
) : BaseClass(userSessionId) {
I can't define it as a data class because of userSessionId, which Kotlin requires to be a val or var in data classes. 由于userSessionId,我无法将其定义为数据类,Kotlin要求在数据类中将其设为val或var。 However, if I do so, then Retrofit throws an exception because there are 2 members named userSessionId.
但是,如果我这样做,则Retrofit会引发异常,因为有2个名为userSessionId的成员。 Is there a way to have a data class inherit from a Java class with a constructor taking arguments?
有没有办法让数据类从Java类继承,而构造函数接受参数呢? Note that I cannot change the base class.
请注意,我无法更改基类。
A possible solution is to define a dummy val to avoid the name clash, but this is less than ideal 一个可能的解决方案是定义一个虚拟val以避免名称冲突,但这不理想。
data class DerivedClass(
val dummy: String,
var other: Other? = null
) : BaseClass(dummy) {
您可以在Java中使用transient
关键字来忽略序列化过程中的字段,这可以在Kotlin中通过使用@Transient
上的@Transient
批注来完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.