Is it possible to override Java getter (method) wi

2020-04-02 05:27发布

问题:

For example:

Java:

public class Foo {

    public int getSomething() {
        return 1;
    }

}

Kotlin:

class Bar : Foo() {

    // works
    override fun getSomething() = 2

    // doesn't work ('something' overrides nothing)
    // override val something = 2

}

I thought that val something = 2 will be transformed to public int getSomething() { return 2; } in Java bytecode.

回答1:

This seems to be a known issue here. Apparently it's a complicated matter and not likely to be resolved anytime soon.

The original response on the issue from Andrey Breslav:

This is a rather deep issue, unfortunately. It's unlikely that we'll ever make it work the way you'd like

Further down on the issue page you can see that it got even more complicated in regards to multiplatform projects.



标签: java kotlin