简体   繁体   English

Kotlin主要构造函数调用次要构造函数

[英]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: 这里有几处错误:

  • Classes should always use camel-case for their names ( test -> Test ) 类的名称应始终使用驼峰式大小写( test > Test
  • You cannot call another constructor as you tried to (calling 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.

相关问题 Kotlin 次构造器和主构造器 - Kotlin Secondary Constructor and Primary Constructor 在Kotlin中将Secondary构造函数调用为Secondary构造函数 - Calling Secondary constructor to Secondary Constructor in Kotlin 用主构造函数和辅助构造函数扩展Kotlin类 - Extending a Kotlin class with a primary and secondary constructor 为什么每个辅助构造函数都需要委托给Kotlin中的主要构造函数? - Why each secondary constructor needs to delegate to the primary constructor in Kotlin? 当主构造函数是泛型时,如何在 Kotlin 中创建具体的辅助构造函数? - How to make a concrete secondary constructor in Kotlin, when the primary constructor is generic? 是否有一种惯用的方法让(辅助)构造函数派生出Kotlin中主要构造函数的值? - Is there not an idiomatic way to have a (secondary) constructor that derives values for the primary constructor in Kotlin? 如何在辅助构造函数kotlin中访问主构造函数参数 - How to access primary constructor parameters inside secondary constructor kotlin Kotlin构造函数(主要构造函数) - Kotlin constructor (primary constructor) 是Kotlin中的constructor()主构造函数吗? - Is constructor() primary constructor in Kotlin? 完成一些计算后从辅助构造函数调用主构造函数 - Calling primary constructor from secondary constructor after some calculations are done
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM