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()
}
}
在上面的片段中对我没有意义的是这一行app.installFeature(Authentication)
谁能向我解释为什么使用class
名称而不是companion object
名称就像更明显的方式一样工作app.installFeature(Authentication.Feature)