Kotlin使用主构造函数实例化开放类

huangapple go评论64阅读模式
英文:

Kotlin instantiate open class with primary construction

问题

I have a simple scenario in which I have bank accounts from multiple countries. Some information is basic, and each country may have their own particularities. Therefore, I created an open class called DefaultBankAccount and I can have country-specific classes inherit them. However, I am not being able to instantiate a simple DefaultBankAccount object.

When I do, the properties in the constructor are empty:

@Document(collection = "bankAccount")
open class DefaultBankAccount(
    accountNumber: String,
    address: String,
    bankName: String,
    codeIban: String,
    codeSwift: String,
    country: String,
    holderName: String
) {
    companion object {
        @Transient
        val SEQUENCE_NAME = "payments_sequence"
    }

    @Id
    var id: Long = 0
    var createdAt: LocalDateTime = LocalDateTime.now()
    var updatedAt: LocalDateTime = LocalDateTime now()
}

Using the primary constructor, the result is:

Kotlin使用主构造函数实例化开放类

So none of the properties on the primary constructor are initialized. However, if I instantiate a class that inherits from DefaultBankAccount, it seems fine:

@Document(collection = "bankAccount")
data class BrazilBankAccount(
    val accountNumber: String,
    val address: String,
    val bankName: String,
    val codeIban: String,
    val codeSwift: String,
    val country: String,
    val holderName: String,
    val bankCode: String,
    val branch: String,
    val digit: String,
    val type: String,
    val ispb: String,
    val docType: String,
    val docNumber: String
) : DefaultBankAccount(accountNumber, address, bankName, codeIban, codeSwift, country, holderName) {
    companion object {
        @Transient
        val SEQUENCE_NAME = "payments_sequence"
    }
}

Kotlin使用主构造函数实例化开放类

My question is: how do I make it so that I can instantiate a DefaultBankAccount object with all its properties?

英文:

I have a simple scenario in which I have bank accounts from multiple countries. Some information is basic, and each country may have their own particularities. Therefore, I created an open class called DefaultBankAccount and I can have country-specific classes inherit them. However, I am not being able to instantiate a simple DefaultBankAccount object.

When I do, the properties in the constructor are empty:

@Document(collection = "bankAccount")
open class DefaultBankAccount(
    accountNumber: String,
    address: String,
    bankName: String,
    codeIban: String,
    codeSwift: String,
    country: String,
    holderName: String
) {
    companion object {
        @Transient
        val SEQUENCE_NAME = "payments_sequence"
    }

    @Id
    var id: Long = 0
    var createdAt: LocalDateTime = LocalDateTime.now()
    var updatedAt: LocalDateTime = LocalDateTime.now()
}

Using the primary constructor, the result is:

Kotlin使用主构造函数实例化开放类

So none of the properties on the primary constructor are initialized. However, if I instantiate a class that inherits from DefaultBankAccount, it seems fine:

@Document(collection = "bankAccount")
data class BrazilBankAccount(
    val accountNumber: String,
    val address: String,
    val bankName: String,
    val codeIban: String,
    val codeSwift: String,
    val country: String,
    val holderName: String,
    val bankCode: String,
    val branch: String,
    val digit: String,
    val type: String,
    val ispb: String,
    val docType: String,
    val docNumber: String
) : DefaultBankAccount(accountNumber, address, bankName, codeIban, codeSwift, country, holderName) {
    companion object {
        @Transient
        val SEQUENCE_NAME = "payments_sequence"
    }
}

Kotlin使用主构造函数实例化开放类

My question is: how do I make it so that I can instantiate a DefaultBankAccount object with all its properties?

答案1

得分: 1

DefaultBankAccount的构造函数中没有声明属性,因为在这些参数声明前面没有valvar关键字。在当前状态下,这些构造函数参数是无用的,因为它们在类主体中没有被使用。

在子类BrazilBankAccount中,构造函数参数都在前面加了val关键字,这将它们变成了属性。

如果您需要DefaultBankAccount也拥有这些属性,请在它们的前面加上val关键字。请注意,您需要在所有子类构造函数属性上加上override修饰符。

英文:

> the properties in the constructor are empty

There are no properties declared in the constructor of DefaultBankAccount, because there is no val or var keyword in front of those parameter declarations. In the current state of things, those constructor parameters are useless, because they are not used anywhere in the class body.

In the child class BrazilBankAccount, the constructor parameters all have the val keyword in front, which makes them properties.

If you need DefaultBankAccount to have those properties too, add a val keyword in front of each of them. Note that you'll need an override modifier on all the child class's constructor properties.

huangapple
  • 本文由 发表于 2023年4月10日 23:01:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978216.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定