Java兼容性的公共字段 [英] Public fields for Java compatibility

查看:80
本文介绍了Java兼容性的公共字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现最近对Kotlin语言感兴趣,因为我们开发的平台基于Java 6,因此缺乏Java带来的语法语法.

I found recent interest in Kotlin as a language, because the platform we develop for is Java 6 based and hence lacks any syntactic sugar the recent years brought to Java.

有一件事使得在开发中无法在Java上使用Kotlin,也就是说,我们开发的平台在内部使用了一些反射,并要求成员公开.否则它将无法正常工作.

There's but one thing that makes it impossible to use Kotlin over Java in development, that is, the platform we develop for uses some reflection internally and requires members to be public. It won't work otherwise.

因此,从Kotlin文件生成的字节码实际上会产生公共获取器和设置器,而字段本身仍是私有的.

So, the bytecode generated from the Kotlin file in fact produces public getters and setters, the fields themselves yet remain private.

有没有一种方法可以克服这个问题,所以我可以获得真正的公共领域?

我知道需要公共领域的设计失败,但是该系统对我们来说是一个黑匣子,我们不能改变必须采用这种方式的事实.

I am aware of the design failure that requires public fields, but the system is kind of a black box to us, we cannot change the fact that it has to be this way.

推荐答案

注释@JvmField应该可以为您提供帮助.它使Kotlin编译器将属性公开为JVM上的字段.参见此处: https://kotlinlang.org/api/最新/jvm/stdlib/kotlin.jvm/-jvm-field/

The annotation @JvmField should help you. It makes the Kotlin-compiler expose the property as a field on the JVM. See here: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.jvm/-jvm-field/

这篇关于Java兼容性的公共字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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