Extend data class in Kotlin

后端 未结 8 2012
日久生厌
日久生厌 2020-11-28 04:31

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 convenien

8条回答
  •  心在旅途
    2020-11-28 04:42

    Declare properties in super-class outside of constructor as abstract, and override them in sub-class.

    abstract class Resource {
        abstract var id: Long
        abstract var location: String
    }
    
    data class Book (
        override var id: Long = 0,
        override var location: String = "",
        var isbn: String
    ) : Resource()
    

提交回复
热议问题