supportFragmentManager 变量在哪里定义?

Where is the supportFragmentManager variable defined?

我正在使用 Kotlin 进行开发。当我到达官方 Android 教程的片段部分时,我遇到了 supportFragmentManager。在 Kotlin 中可用作变量,而在 java 中我们可以调用其等效方法 getSupportFragmentManager()

我想知道 supportFragmentManager 变量在哪里定义,因为我看不到任何类似具有该名称的变量声明的东西,但是单击该变量将我带到 fragmentActivity.java [=23] 中的以下方法=].

/**
 * Return the FragmentManager for interacting with fragments associated
 * with this activity.
 */
public FragmentManager getSupportFragmentManager() {
    return mFragments.getSupportFragmentManager();
}

如何在 Kotlin 中将此方法作为变量访问,而在 java 中我们必须像常规方法一样访问?任何帮助将不胜感激。

实际上,在 Kotlin 中,当您调用 supportFragmentManager 时,它不是可变的,任何 Java 方法在 Kotlin 的方法中包含 get 前缀(不带参数),它将被调用为没有 get 字的变量

Methods that follow the Java conventions for getters and setters (no-argument methods with names starting with get and single-argument methods with names starting with set) are represented as properties in Kotlin. Boolean accessor methods (where the name of the getter starts with is and the name of the setter starts with set) are represented as properties which have the same name as the getter method.

阅读更多关于 here

这类似于 Kotlin 中的 getter 和 setter 方法。您不需要将 getProperty()setProperty() 方法描述为 属性 的 access/update 值。

您可以通过此 中的示例了解更多关于它实际如何工作的信息。

希望对您有所帮助。 编码愉快..!