Data classes seem to be the replacement to the old-fashioned POJOs in Java. It is quite expectable that these classes would allow for inheritance, but I can see no convenient way to extend a data class. What I need is something like this:
open data class Resource (var id: Long = 0, var location: String = "")
data class Book (var isbn: String) : Resource()
The code above fails because of clash of component1()
methods. Leaving data
annotation in only one of classes does not do the work, too.
Perhaps there is another idiom to extend data classes?
UPD: I might annotate only child child class, but data
annotation only handles properties declared in the constructor. That is, I would have to declare all parent's properties open
and override them, which is ugly:
open class Resource (open var id: Long = 0, open var location: String = "")
data class Book (
override var id: Long = 0,
override var location: String = "",
var isbn: String
) : Resource()
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…