使用接受Kotlin中受保护类的公共方法从Java类继承 [英] Inheritance from Java class with a public method accepting a protected class in Kotlin
问题描述
我有这种情况:有一个Java类
I have this situation: There are a Java class
public class A {
public void overrideMe(B param){
//TODO: override me in Kotlin!
}
protected static class B {
}
}
和一个继承自它的Kotlin类,并且必须重写方法"overrideMe"
and a Kotlin class, which inherits from it and has to override method "overrideMe"
class K: A() {
override fun overrideMe(param: B) {
println("Wow!")
}
}
但是Kotlin不允许这种行为.
But Kotlin doesn't allow this behaviour.
"public"函数公开其受保护的(在A中)"参数类型B
'public' function exposes its 'protected (in A)' parameter type B
有什么办法解决这个问题吗?
Is there any way how to resolve this one?
P.S. It's not just a synthetic case - I faced this problem when I tried to implement custom Spring AmqpAppender and to override it's postProcessMessageBeforeSend method.
推荐答案
毕竟,结论是:纯Kotlin无法解决这种情况.
Well, after all, the conclusion is: there is no way to solve this situation in pure Kotlin.
我希望AmqpAppender.Event在不久的将来公开.
I hope that AmqpAppender.Event will become public in the nearest future.
即使Java允许这种行为,对我(对于Kotlin的开发人员)来说,在公共方法中包含不公开的参数似乎也是一个糟糕的设计.
Even if Java allows that behaviour, having no-public arguments in public methods seems like a bad design for me (also for the developers of Kotlin).
这篇关于使用接受Kotlin中受保护类的公共方法从Java类继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!