【问题标题】:I Can't call methods in kotlin我不能在 kotlin 中调用方法
【发布时间】:2020-09-22 07:14:17
【问题描述】:

我是 kotlin 的新手,刚开始学习类和方法 但在下面的代码中我不能调用makeReddit() 方法:

class Site( address: String, foundationYear: Int) {
    var address: String = address
    var foundationYear: Int = foundationYear
    
    fun makeReddit(address: String , foundationYear: Int) {
        this.address = "reddit.com"
        this.foundationYear = 2005
    }
}

fun main() {
    val site : Site = makeReddit()
}

我得到了这个错误:

Kotlin: Unresolved reference: makeReddit

【问题讨论】:

  • 你必须先初始化Site,然后调用site.makeReddit("reddit.com", 2005)这样的方法,但看起来那个方法只是设置变量,没有做任何其他事情,所以你可以只使用data class Site(address: String, foundationYear: Int) 并通过构造函数进行初始化:val site = Site("reddit.com", 2005)。取决于您要对该网站和年份做什么。
  • 我强烈建议您阅读一些 Kotlin 教程(甚至可能是 java/oop 教程)。您可以从这些tutorials 开始,首先您需要掌握有关 kotlin/oop 工作原理的概念。否则你会在 StackOverflow 上问很多关于为什么你的代码不能编译的问题。
  • 这是 JetBrains Academy Kotlin Course 上的一个任务,任务只是说有这个类:class Site(val address: String, val foundationYear: Int) 实现 makeReddit() 函数,该函数返回一个带有 reddit.com 地址和基金会的站点2005 年。
  • 您的 makeReddit() 方法目前没有返回任何内容...您必须声明它 fun makeReddit(address: String , foundationYear: Int) : Site { ... } 并在正文中真正返回 Site...
  • “JetBrains Academy Kotlin 课程”可能对您现在来说太高级了。首先你需要了解什么是class,什么是function,以及它能做什么。例如,在您的情况下,任务是添加一个应该 返回 的函数,但您没有返回任何内容,这可能表明您不熟悉 return 概念。

标签: class oop kotlin methods


【解决方案1】:

在 Kotlin 中,函数不需要在某个类中声明。在类之外声明的函数称为顶级函数。

在此任务中,您可以将makeReddit 声明为Site 类旁边的顶级函数:

class Site(val address: String, val foundationYear: Int) {
    // ...
}

fun makeReddit(): Site {
    // create and return "reddit" site
}

然后你就可以像makeReddit()一样从其他地方调用它,例如从main函数:

fun main() {
    val reddit: Site = makeReddit()
    println(reddit.address)
}

【讨论】:

    【解决方案2】:

    这是 JetBrains Academy Kotlin 课程上的一个任务,该任务只是说
    有这个课程:class Site(val address: String, val foundationYear: Int)
    实现makeReddit() 函数,该函数返回一个Site,其中包含reddit.com 地址和2005 年的基础年份。

    (你的评论)

    这看起来像是针对使用companion object 的任务,因为您需要一个方法makeReddit(),您可以在没有Site 实例的情况下使用它,例如Site.makeReddit()

    你可以这样做:

    class Site(val address: String, val foundationYear: Int) {
        var url: String = address
        var founded: Int = foundationYear
        
        // provide a singleton that creates an instance
        companion object SiteFactory {
            // the fun to be used without an instance of Site
            fun makeReddit() : Site {
                // return the (fix) values of reddit.com
                return Site("reddit.com", 2005)
            }
        }
        
        // provide a String representation
        override fun toString(): String {
            return "$url founded in $founded"
        }
    }
    

    然后您可以(至少)以两种不同的方式创建Site 的实例:

    fun main() {
        // create the reddit site instance directly from Site 
        val reddit = Site.makeReddit()
        // create an instance with different values via constructor
        val random = Site("ran.dom", 2008)
        // and print both of them
        println("reddit: $reddit")
        println("random: $random")
    }
    

    fun main() 将输出

    reddit: reddit.com founded in 2005
    random: ran.dom founded in 2008
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-15
      • 1970-01-01
      • 1970-01-01
      • 2020-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多