简体   繁体   English

是否可以使用Kotlin val(属性)覆盖Java getter(方法)?

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

For example: 例如:

Java: 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; } 我认为val something = 2将被转换为public int getSomething() { return 2; } public int getSomething() { return 2; } in Java bytecode. Java字节码中的public int getSomething() { return 2; }

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: 安德烈·布雷斯拉夫(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. 在问题页面的更下方,您可以看到它在多平台项目方面变得更加复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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