我知道私有(private)方法不能被重写,但这是我的问题。
A_CLASS_FROM_3RD_PARTY_LIBRARY_THAT_I_CANNOT_MODIFY
来 self 无法修改的 3rd 方库:
open class A_CLASS_FROM_3RD_PARTY_LIBRARY_THAT_I_CANNOT_MODIFY {
private fun print() {
println("A")
}
open public fun run() {
// A lot of code here
// A lot of code here
// A lot of code here
print()
// A lot of code here
// A lot of code here
// A lot of code here
}
}
我唯一想要更改的是其中的 print()
方法。有没有比覆盖整个 run() 方法并复制并粘贴 super 类中的所有代码更好的方法,以便覆盖私有(private) print() 方法?例如,
class B : A_CLASS_FROM_3RD_PARTIES_THAT_I_CANNOT_MODIFY() {
private fun print() {
println("B")
}
// Copy & paste the whole run() method from supper class
override public fun run() {
// A lot of code here
// A lot of code here
// A lot of code here
print()
// A lot of code here
// A lot of code here
// A lot of code here
}
}
最佳答案
无论是在 Kotlin 还是在 Java 中,都不可能重写私有(private)方法。
关于java - 在 Kotlin 中重写私有(private)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68045242/