【发布时间】:2018-11-06 04:22:43
【问题描述】:
看看这段代码,它模仿了features 在Ktor 应用程序中的安装方式。
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()
}
}
上面的 sn-p 对我来说没有意义的是这一行 app.installFeature(Authentication)
谁能向我解释为什么使用class 名称而不是companion object 名称就像app.installFeature(Authentication.Feature) 更明显的方式一样工作
【问题讨论】: