【发布时间】:2018-02-20 21:43:48
【问题描述】:
我的类有一个私有构造函数,并为某种“通用构造函数”在伴随对象上实现了调用
class Test private constructor(className: String) {
companion object {
// If I remove the internal it fails
internal inline operator fun <reified T> invoke(): Test {
return Test(T::class.java.name) // why can I even call it? The constructor is private
}
}
}
我什至可以拥有一个调用通用构造函数的公共内联函数
public inline fun test() = Test<Any>() // Why can I call it, it is internal
这不应该意味着每次调用 test() 都会扩展为 Test(Any::class.java.name),即使该构造函数是私有的?
所以我的问题是:
- 为什么
internal inline fun可以调用私有构造函数? (公共乐趣不能) - 为什么
public inline fun可以调用内部函数? - 为什么我最终可以在
public inline fun中公开私有构造函数?
【问题讨论】:
标签: kotlin