问题
While migration to Kotlin
from Java
I faced with a problem. I overrided Object
's finalize()
method:
@Override
protected void finalize() throws Throwable {
stopTimer();
super.finalize();
}
When I tried to do the same with Kotlin I found to solutions. The first one is from the doc:
protected fun finalize() {
stopTimer()
super.finalize()
}
And the second one from the article (it's in Russian):
@Suppress("ProtectedInFinal", "Unused")
protected fun finalize() {
stopTimer()
super.finalize()
}
But in both cases I can't call super.finalize()
according to IDE, as it says unresolved reference:finalize
Maybe anybody knows how to get this work in Kotlin
? Thanks.
回答1:
Here's the contract of finalize
in Java:
The finalize method of class Object performs no special action; it simply returns normally. Subclasses of Object may override this definition.
Therefore you are not required to call through to the superclass. You would be calling through to an empty implementation.
The need to call super.finalize()
arises only in classes not directly deriving from kotlin.Any
.
The rest of the story is already told in the official documentation: just declare a protected fun finalize()
.
来源:https://stackoverflow.com/questions/50393502/kotlin-super-finalize