For example, we have this parent:
open class Parent(val id: Int, val name: String?) {
constructor() : this(-1, null)
}
And a child, which must have both a two-param constructor and an empty constructor, like the parent:
class Child(id: Int, name: String?) : Parent(id, name) {
constructor() : super() // syntax error
}
子构造函数如何使用其父构造函数的二级构造函数?
I am aware that I can implement a child constructor passing in the same values as the parent, but this not only seems redundant but also often times my childs have extra parameters for its primary constructor, but don't require intermediary constructors (constructors with params that don't include all the extra params). Here's an example child implementing it that way, in case I wasn't clear:
class Child(id: Int, name: String?) : Parent(id, name) {
constructor() : this(-1, null) // no syntax error, but redundant
}