加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

单身人士作为Scala的合成课程?

发布时间:2020-12-16 09:35:34 所属栏目:安全 来源:网络整理
导读:我正在阅读Scala中的编程,我不明白以下句子(pdf p.112): Each singleton object is implemented as an instance of a synthetic class referenced from a static variable,so they have the same initialization semantics as Java statics. 这是否意味着
我正在阅读Scala中的编程,我不明白以下句子(pdf p.112):

Each singleton object is implemented as an instance of a synthetic class referenced from a
static variable,so they have the same initialization semantics as Java statics.

这是否意味着如果我在scala中有一个单例FooBar,编译器会创建一个名为FooBar $的类?

另外,作者的意思是“从静态变量引用”?有一个隐藏的静态变量在某个地方持有一些FooBar $类的引用?

我感谢任何帮助。

解决方法

同样的“Scala编程”第31章更准确:

Java has no exact equivalent to a singleton object,but it does have static methods.

The Scala translation of singleton objects uses a combination of static and instance methods. For every Scala singleton object,the compiler will create a Java class for the object with a dollar sign added to the end.
For a singleton object named App,the compiler produces a Java class named App$.
This class has all the methods and fields of the Scala singleton object.
The Java class also has a single static field named MODULE$ to hold the one
instance of the class that is created at run time.
As a full example,suppose you compile the following singleton object:

object App {
  def main(args: Array[String]) {
    println("Hello,world!")
  }
}

Scala will generate a Java App$ class with the following fields and methods:

$ javap App$
public final class App$ extends java.lang.Object
    implements scala.ScalaObject{
  public static final App$ MODULE$;
  public static {};
  public App$();
  public void main(java.lang.String[]);
  public int $tag();
}

That’s the translation for the general case.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读