java - 在 Kotlin 中重写私有(private)方法

标签 java android kotlin

我知道私有(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/

相关文章:

java - Spring Boot 程序找不到主类

Java Bean 验证 : How do I specify multiple validation constraints of the same type but with different groups?

android - 类型转换为 Long

java - 使用线程和 USB 连接后应用程序屏幕空白

android - 无法从 Firestore 获取数据,没有错误

java - Swing - 删除 JMenu 中除最后一个 JMenuItem 之外的所有内容

java - WindowBuilder 设计模式未显示我的 Widget 的视觉特征

android - ViewPager 不显示任何内容

kotlin - 具有自己的协程范围的单元测试类

android - 下拉菜单剪辑/缩小项目 Jetpack Compose