英文:
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:
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"
}
}
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:
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"
}
}
My question is: how do I make it so that I can instantiate a DefaultBankAccount object with all its properties?
答案1
得分: 1
在DefaultBankAccount
的构造函数中没有声明属性,因为在这些参数声明前面没有val
或var
关键字。在当前状态下,这些构造函数参数是无用的,因为它们在类主体中没有被使用。
在子类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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论