简体   繁体   中英

Is it possible to override Java getter (method) with Kotlin val (property)?

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; } public int getSomething() { return 2; } in Java bytecode.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM