我想从另一个类中的 MainActivity 类调用扩展函数。 我该怎么做?
object MainActivity : AppCompatActivity() {
val StringBuilder.readHistory: StringBuilder
get() {
val temp = this@readHistory
temp.setLength(0)
try {
val file = InputStreamReader(MainActivity.openFileInput(MainActivity.getString(R.string.dosyaadı)))
val br = BufferedReader(file)
var line = br.readLine()
while (line != null) {
temp.append(line + "\n")
line = br.readLine()
}
br.close()
file.close()
} catch (e: Exception) {
e.printStackTrace()
}
return temp
}
最佳答案
你不能在类之外调用它,因为它嵌套在这个类中,因此只适用于那个范围。
使扩展属性成为顶级(将其移出类)。
关于android - Kotlin 如何调用扩展函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51125445/