【问题标题】:Error: None of the following functions can be called with the arguments supplied错误:不能使用提供的参数调用以下函数
【发布时间】:2019-06-19 23:09:11
【问题描述】:

我的班级:

class Manager (var name: String, var nationality: String) {

    constructor(agent: String): this() {}
}

返回以下错误:

None of the following functions can be called with the arguments supplied.

<init>(String) defined in Manager
<init>(String, String) defined in Manager

知道为什么吗?

【问题讨论】:

  • 提示:this() 调用的是什么构造函数?
  • 你能解释一下this()在这里做什么吗?我从教程中获取了代码。

标签: kotlin


【解决方案1】:

你的类有一个带两个参数的主构造函数,然后你定义一个带一个参数的辅助构造函数。

现在,根据 Kotlin documentation

如果类有一个主构造函数,则每个辅助构造函数 需要直接或直接委托给主构造函数 间接通过另一个辅助构造函数。

您试图通过调用 this() 来实现这一点,但由于您没有零参数构造函数(主要或次要),这会导致编译错误。

例如,要修复,您可以从辅助构造函数调用主构造函数,如下所示:

class Manager (var name: String, var nationality: String) {
    constructor(agent: String): this(agent, "") {}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-07
    • 1970-01-01
    • 2017-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多