简体   繁体   中英

Primitive properties for a class in init block - Kotlin

I'm defining a Kotlin class with a number of primitive properties:

class Contract (contractEntity : ContractEntity) EntityDao<ContractEntity> {
    var id : Long // <- This is a primitive datatype, needs to be initialized
    var concept : String //<- This also needs to be initialized or declared abstract

    init{
        mapFromEntity(contractEntity)
    }

    override fun mapFromEntity(entity : ContractEntity){
        id = entity.id
        concept = entity.concept
    }
}

Now, I want those properties to be initialized with the function mapFromEntity() but I'm stuck with the init block because those are not initialized. What could be a good way to achieve what I'm trying?

So far, the best solution suggest to remove the function that maps the class and use the constructor parameter.

class Contract (contractEntity : ContractEntity) EntityDao<ContractEntity> {
    var id = contractEntity.id
    var concept = contractEntity.concept
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM