Java 静态类成员和 Scala 互操作性 [英] Java static class members and Scala interoperability

查看:53
本文介绍了Java 静态类成员和 Scala 互操作性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自Scala 编程语言概述,第二版:

//Scala对象打印选项{def main(args: Array[String]): Unit = {System.out.println("选项选择:")for (val arg <- args)if (arg.startsWith("-"))System.out.println(" " + arg.substring(1))}}

<块引用>

在上面的例子中,Scala 程序调用方法 startsWithStringsubstring,它是Java中定义的一个类.它还访问 Java 类 System 的静态 out 字段,以及调用它的(重载的)println 方法.这甚至是可能的尽管 Scala 没有静态类成员的概念.实际上,每个 Java 类在 Scala 中都被视为两个实体,一个类包含所有动态成员和一个单例对象,包含所有静态成员.

我理解将 Scala 的伴随对象翻译成 Java 字节码,但我不确定在上面的块引用在 Scala 中看到"中的粗体文本究竟是什么意思(从 Java 到 Scala)).

这是否意味着带有静态成员的 Java 类实际上在 Scala 中被转换或解释为两个实体?或者我的两个假设都是错误的?

解决方案

我认为您可能会被 Java 假设蒙蔽了双眼.考虑这个简单的代码片段:

X.Y()

这意味着方法 Y 正在对象 X 上被调用,或者在 X 隐式转换成的某个其他对象上被调用.

也许这看起来并不奇怪,或者您看不出有什么不妥,所以让我们明确说明一个结果:X 将永远成为一个类.您不会在类上调用方法,期间.

自然,这在静态成员方面与 Java 存在严重的互操作性问题,这就是为什么声明 Java 类 X 的静态成员将被视为"单例对象的原因:因为,否则,您将永远无法使用它们.

请注意,Scala 的单例对象是真正的对象——它们是单例类的实例.但是,具有静态成员的 Java 类不会为单个对象提供来源.实际上,这意味着这一行:

val x = X

如果 X 是一个 Scala 单例对象,将起作用,但如果它是一个具有静态成员的 Java 类,则不起作用.

From the An Overview of the Scala Programming Language, Second Edition:

// Scala
object PrintOptions {
    def main(args: Array[String]): Unit = {
        System.out.println("Options selected:")
        for (val arg <- args)
            if (arg.startsWith("-"))
                System.out.println(" " + arg.substring(1))
    }
}

In the example above, the Scala program invokes methods startsWith and substring of String, which is a class defined in Java. It also accesses the static out field of the Java class System, and invokes its (overloaded) println method. This is possible even though Scala does not have a concept of static class members. In fact, every Java class is seen in Scala as two entities, a class containing all dynamic members and a singleton object, containing all static members.

I understand translation of Scala's companion objects into Java bytecode, but I am not sure what exactly does it means bold text in upper blockquote "is seen in Scala" for opposite example (from Java to Scala).

Does it mean that Java classes with static members are actually converted or just interpreted as two entities in Scala? Or both of my assumptions are wrong?

解决方案

I think you might be blinded by Java assumptions. Consider this simple snippet of code:

X.Y()

The means that the method Y is being called on the object X, or on some other object which X was implicitly converted into.

Maybe that doesn't look surprising, or you don't see anything amiss with that, so let's state explicitly a consequence: X will NEVER be a class. You don't invoke methods on classes, period.

Naturally, that presents a serious interoperability problem with Java regarding static members, and that's why it is stated that static members of a Java class X will be "seen" as a singleton object: because, otherwise, you'll never be able to use them.

Note that Scala's singleton objects are true objects -- they are instances of singleton classes. Java classes with static members will not give origin to single objects, though. In practice, this means that this line:

val x = X

will work if X is a Scala singleton object, but won't work if it is a Java class with static members.

这篇关于Java 静态类成员和 Scala 互操作性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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