我想要从类 SomeClass
外部调用该类的私有函数:
class SomeClass {
private fun somePrivateFunction() {
//...
}
private fun somePrivateFunctionWithParams(text: String) {
//...
}
}
在代码的某个地方,我引用了一个SomeClass
对象:
val someClass = SomeClass()
// how can I call the private function `somePrivateFunction()` from here?
// how can I call the private function `somePrivateFunctionWithParams("some text")` from? here
如何在 Kotlin 中从类外部调用带参数和不带参数的私有函数?
version
的函数? - Sergioinline fun <reified T : Any, R> T.setPrivateProperty(name: String, value: R) = T::class .memberProperties .firstOrNull { it.name == name } ?.apply { isAccessible = true } ?.let { (it as? KMutableProperty1<T, R>)?.setter?.call(this, value) }
- undefined