0

对不起,我是 kotlin 的新手,所以请多多包涵。

我有这个代码

 Class A :Basefragment()
    {
        ...

        override fun onOptionsItemSelected(item: MenuItem): Boolean {
            val id = item.itemId

            if (id == R.id.save) {
                val thread = SimpleThread(editTitle.text.toString(), editDescription.text.toString())
                thread.start()
            }
        }

        inner class SimpleThread(title: String, description: String) : Thread() {
            override fun run() {
                var titles = title // how to use title ?
            }

        }
    }

在 SimpleThread 中,如何获取标题值?我得到未解决的参考

4

1 回答 1

2

您当前的语法仅传入titledescription作为构造函数参数,您可以使用它来初始化属性,或者在init块中:

inner class SimpleThread(title: String, description: String) : Thread() {
    val title = title

    init {
        println(description)
    }
}

虽然您可以将这些值保存到如上所示的属性中,但您也可以直接添加val或添加var到构造函数以创建采用构造函数参数值的属性:

inner class SimpleThread(val title: String, val description: String) : Thread() { ... }

现在可以随时从任何函数访问这些保存的属性,而不仅仅是在构造时。

于 2019-02-28T05:55:56.860 回答