Kotlin语法:使用类名而不是伴随对象名之间的区别

Kotlin语法:使用类名而不是伴随对象名之间的区别,kotlin,ktor,Kotlin,Ktor,看看这段代码,它模仿了应用程序中的安装方式 fun main(args: Array<String>) { val app = App() app.installFeature(Authentication) } interface AppFeature { fun install() } class Authentication { companion object Feature : AppFeature { override

看看这段代码,它模仿了应用程序中的安装方式

fun main(args: Array<String>) {
    val app = App()
    app.installFeature(Authentication)
}

interface AppFeature {
    fun install()
}

class Authentication {

    companion object Feature : AppFeature {
        override fun install() = println("Authentication Installed")
    }
}

class App {

    fun installFeature(appFeature: AppFeature) {
        println("Installing appFeature `${appFeature::class.simpleName}`")
        appFeature.install()
    }
}
fun main(args:Array){
val app=app()
app.installFeature(身份验证)
}
接口应用程序功能{
趣味安装()
}
类身份验证{
伴生对象特征:AppFeature{
重写fun install()=println(“已安装身份验证”)
}
}
类应用程序{
趣味installFeature(appFeature:appFeature){
println(“安装appFeature`${appFeature::class.simpleName}`”)
appFeature.install()
}
}
在上面的代码片段中,对我来说没有意义的是这一行
app.installFeature(身份验证)

有谁能向我解释一下,为什么使用
名称而不是
伴生对象
名称的工作方式与下面提到的更明显的
app.installFeature(Authentication.Feature)

一样:

只需使用类名作为限定符,就可以调用伴随对象的成员

同样,您可以直接将
身份验证
用作
AppFeature

如以下文件中所述:

只需使用类名作为限定符,就可以调用伴随对象的成员

同样,您可以直接将
身份验证
用作
AppFeature