Java 中 Scala 对象的等价物是什么?

发布于 2024-12-10 14:38:24 字数 531 浏览 0 评论 0原文

在Scala中,我们可以写

object Foo { def bar = {} }

编译器是如何实现的?我可以从 Java 调用 Foo.bar(); 但是 Java 中的 new Foo(); 给出错误 cannot find symbol symbol: constructor Foo()

  • JVM 本身是否支持单例?
  • Java中是否可以有一个没有构造函数的类?

注意:这里是 scalac -print 输出的代码

package <empty> {
  final class Foo extends java.lang.Object with ScalaObject {
    def bar(): Unit = ();
    def this(): object Foo = {
      Foo.super.this();
      ()
    }
  }
}

In Scala, we can write

object Foo { def bar = {} }

How is this implemented by the compiler? I am able to call Foo.bar(); from Java
but new Foo(); from Java gives the error cannot find symbol symbol: constructor Foo()

  • Does the JVM support singletons natively?
  • Is it possible to have a class in Java that does not have a constructor?

Note: here is the code output by scalac -print

package <empty> {
  final class Foo extends java.lang.Object with ScalaObject {
    def bar(): Unit = ();
    def this(): object Foo = {
      Foo.super.this();
      ()
    }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

毁梦 2024-12-17 14:38:24

编译代码时,Scala 编译器会生成与以下 Java 代码等效的代码:

public final class Foo {
    private Foo() {} // Actually, Foo doesn't have constructor at all
                     // It can be represented in bytecode, 
                     // but it cannot be represented in Java language

    public static final void bar() {
        Foo$.MODULE$.bar();
    }
}

public final class Foo$ implements ScalaObject {
    public static final Foo$ MODULE$;
    static {
        new Foo$();
    }
    private Foo$() { MODULE$ = this; }
    public final void bar() {
        // actual implementation of bar()
    }
}

这里 Foo$ 是单例的实际实现,而 Foo 提供 static< /code> 与 Java 交互的方法。

When compiling your code, Scala compiler produces an equivalent of the following Java code:

public final class Foo {
    private Foo() {} // Actually, Foo doesn't have constructor at all
                     // It can be represented in bytecode, 
                     // but it cannot be represented in Java language

    public static final void bar() {
        Foo$.MODULE$.bar();
    }
}

public final class Foo$ implements ScalaObject {
    public static final Foo$ MODULE$;
    static {
        new Foo$();
    }
    private Foo$() { MODULE$ = this; }
    public final void bar() {
        // actual implementation of bar()
    }
}

Here Foo$ is an actual implementation of a singleton, whereas Foo provides a static method for interaction with Java.

懒的傷心 2024-12-17 14:38:24

对单例的支持不是在语言级别上,但是该语言提供了足够的设施来毫无困难地创建它们。

考虑以下代码:

public class Singleton {
    private static final Singleton instance = new Singleton();

    // Private constructor prevents instantiation from other classes
    private Singleton() {}

    public static Singleton getInstance() {
        return instance;
    }
}

这是来自维基百科的示例,它解释了如何创建单例。实例保存在私有字段中,构造函数在类外部无法访问,该方法返回此单个实例。

至于构造函数:默认情况下,每个类都有一个所谓的默认构造函数,它不带参数,只是调用超类的无参数构造函数。如果超类没有任何可访问的无参数构造函数,则必须编写显式构造函数。

因此,一个类必须有一个构造函数,但如果超类有一个无参数构造函数,则不必编写它。

Support for singletons is not on a language level, but the language provides enough facilities to create them without any trouble.

Consider the following code:

public class Singleton {
    private static final Singleton instance = new Singleton();

    // Private constructor prevents instantiation from other classes
    private Singleton() {}

    public static Singleton getInstance() {
        return instance;
    }
}

This is an example from Wikipedia, which explains how a singleton can be made. An instance is kept in a private field, constructor is inaccessible outside the class, the method returns this single instance.

As for constructors: every class by default has a so-called default constructor which takes no arguments and simply calls the no-args constructor of the superclass. If the superclass doesn't have any accessible constructor without arguments, you will have to write an explicit constructor.

So a class must have a constructor, but you don't have to write it if the superclass has a no-args constructor.

oО清风挽发oО 2024-12-17 14:38:24

Joshua Bloch 在《Effective Java》一书中推荐使用枚举来实现单例。

看这个问题:
在 Java 中实现单例模式的有效方法是什么?

Joshua Bloch recommened in the book "Effective Java" the use of an enum to implement a singleton.

See this question:
What is an efficient way to implement a singleton pattern in Java?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文