[英]Kotlin primary constructor calling secondary constructor
Why does this not compile? 为什么不编译?
class test
{
constructor() {
var a = Date().day
this(a)
}
constructor(a:Int) {
}
}
error is: Expression 'this' of type 'test' cannot be invoked as a function. 错误是:无法将类型为“ test”的表达式“ this”作为函数调用。 The function 'invoke()' is not found. 找不到函数“ invoke()”。
The suggested fix is to add this: 建议的解决方案是添加以下内容:
private operator fun invoke(i: Int) {}
Why? 为什么?
First, both of these constructors are secondary constructors. 首先,这两个构造函数都是辅助构造函数。 A primary constructor is one which is located outside of the body of the class. 主要构造函数是位于类主体外部的构造函数。
Second, as described in the documentation , the correct syntax to call another constructor is as follows: 其次,如文档中所述,调用另一个构造函数的正确语法如下:
class Test {
constructor() : this(1) { }
constructor(a: Int) { }
}
class test constructor(){ // primary constructor (The primary constructor is part of the class header: it goes after the class name (and optional type parameters))
constructor(a: Int) : this() { // secondary constructor
}
}
If you class have define primary constructor
, secondary constructor
needs to delegate to the primary constructor
. 如果您的类具有定义的primary constructor
,则secondary constructor
需要委托给primary constructor
。 See here . 看这里 。
I think primary constructor
can not be called from secondary constructor
. 我认为不能从secondary constructor
调用primary constructor
secondary constructor
。
You can think like this: secondary calls primary and primary calls secondary => endless loop => not possible 您可以这样想:次要呼叫主要和次要呼叫次要=>无限循环=>不可能
In your case, there are 2 secondary constructor
, so you can do like 在您的情况下,有2个secondary constructor
,因此您可以像
class test {
constructor() : this(Date().day) // I see it quite like Java here https://stackoverflow.com/questions/1168345/why-do-this-and-super-have-to-be-the-first-statement-in-a-constructor
constructor(a: Int) {
}
}
Couple of things are wrong here: 这里有几处错误:
test
-> Test
) 类的名称应始终使用驼峰式大小写( test
> Test
) this(1)
inside of the other constructors body) 您不能像尝试那样调用其他构造函数this(1)
在其他构造函数主体内部调用this(1)
) I think what you actually want is a
being a property and alternatively initialize it with a default value. 我想你真正想要的是a
是一个属性,或者用默认值初始化。 You could do it like this 你可以这样
class Test(val a: Int) {
constructor() : this(1) // notice how you can omit an empty body
}
or even better, like this: 甚至更好,像这样:
class Test(val a: Int = 1) // again an empty body can be omitted.
Edit: 编辑:
If you need to do some calculations, as asked in the comment below Yole's answer: 如果您需要进行一些计算,请按照Yole答案下方的评论中的要求进行:
class Test(val day: Int) {
// you can use any expression for initialization
constructor(millis: Long) : this(Date(millis).day)
}
or if things get more complicated: 或者事情变得更加复杂:
class Test(var day: Int) {
// pass something (i.e. 1) to the primary constructor and set it properly in the body
constructor(millis: Long) : this(1) {
// some code
day = // initialize day
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.