是否可以添加“静态"字词? Java类的扩展功能? [英] Is it possible to add "static" extension function for Java class?

查看:46
本文介绍了是否可以添加“静态"字词? Java类的扩展功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有Java类(来自外部库):

For example I have Java class (from external library):

class A {} // This is Java class

我想添加用Kotlin编写的扩展功能,并将其命名为:

I want to add extension functions written on Kotlin and call it as:

A.foo() // This is call of extension function `foo` from Kotlin code

据我了解,目前在Kotlin中是不可能的,因为它支持

As I understand, right now it is impossible to do in Kotlin because it support "static" extension functions for KClass-es with companion object only. Right?

似乎没有什么可以阻止以后在Kotlin中实现这种功能.对吧?

Seems like nothing to prevent to implement such functionality in Kotlin later. Right?

更新2019-06-12 :此问题不能回答我的问题,因为我的问题是有关Kotlin扩展功能与Java类的兼容性.

UPDATE 2019-06-12: This question doesn't answer to my question because my question about compatibility of Kotlin extension functions with Java classes.

推荐答案

您是对的.在Kotlin 1.0中,您可以在Kotlin类的伴随对象上定义扩展功能,并且可以使用A.foo()语法调用此类功能.支持在Java类上定义静态扩展功能是Kotlin未来版本的一种可能功能,但目前不在Kotlin 1.1的开发路线上.

You're right. In Kotlin 1.0, you can define extension functions on a companion object of a Kotlin class, and such functions can be called using the A.foo() syntax. Support for defining static extension functions on Java classes is a possible feature for future versions of Kotlin, but it's not on the roadmap of Kotlin 1.1 at this time.

这篇关于是否可以添加“静态"字词? Java类的扩展功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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