是否可以使用Kotlin val(属性)覆盖Java getter(方法)? [英] Is it possible to override Java getter (method) with Kotlin val (property)?

查看:151
本文介绍了是否可以使用Kotlin val(属性)覆盖Java getter(方法)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

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

}

我认为 val something = 2 将转换为 public int getSomething(){return 2; Java字节码中的

推荐答案

这似乎是一个已知问题这里。显然这是一个复杂的问题,不太可能很快得到解决。

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.

这篇关于是否可以使用Kotlin val(属性)覆盖Java getter(方法)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆