scala 没有参数的Scala构造函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6874329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 03:16:45  来源:igfitidea点击:

Scala constructor without parameters

scala

提问by HairyFotr

I may be having a silly problem here... I can't seem to figure out how to make a constructor without parameters in Scala. I know I can just write the whole thing in the class body (especially because it's the only constructor I need), but it doesn't quite feel right.

我可能在这里遇到了一个愚蠢的问题......我似乎无法弄清楚如何在 Scala 中创建一个没有参数的构造函数。我知道我可以在类主体中编写整个内容(尤其是因为它是我唯一需要的构造函数),但感觉不太对。

What I have:

我有的:

    class Foo {
        //some init code

        //...
    }

What I'd like (but doesn't work as it wants me to call another constructor first):

我想要什么(但不起作用,因为它希望我先调用另一个构造函数):

    class Foo {
        // The only constructor
        def this() = { 
            //some init code
        }

        //...
    }

回答by Andrzej Doyle

All classes in Scala have a primary constructor and optionally some auxiliary constructors (which must defer to the primary constructor or another auxiliary constructor).

Scala 中的所有类都有一个主构造函数和一些可选的辅助构造函数(必须服从主构造函数或另一个辅助构造函数)。

The issue in your case is that in both cases you've defined the primary constructor as taking no arguments - and then in the second case you try to define an auxiliary constructor with the same signature. This doesn't work, for the same reason that the following wouldn't compile:

您的情况的问题是,在这两种情况下,您都将主构造函数定义为不带参数 - 然后在第二种情况下,您尝试定义具有相同签名的辅助构造函数。这不起作用,原因与以下内容无法编译的原因相同:

// Primary constructor takes a String
class Foo(s: String) {
    // Auxiliary constructor also takes a String?? (compile error)
    def this(a: String) {
        this(a)
    }
}

This isn't anything to do with the fact that the constructor is no-args; the following compiles for example:

这与构造函数是无参数的事实没有任何关系;例如以下编译:

class Foo(s: String) {
    // alternative no-arg constructor (with different signature from primary)
    def this() {
        this("Default value from auxiliary constructor")
    }
}

In particular, in your second example, your comment "the only constructor" is wrong. Auxiliary constructors are alwayssecondary to the primary constructor, and cannot ever be the only constructor.

特别是,在您的第二个示例中,您的注释“唯一的构造函数”是错误的。辅助构造函数总是次于主构造函数,并且永远不可能是唯一的构造函数。

FWIW, the first example is the only option open to you, but it looks fine to me. If you've just started using Scala I'm sure it will start to feel right soon enough - and it's important to eschew Java-esque ways of doing things when there are more idiomatic alternatives.

FWIW,第一个示例是唯一向您开放的选项,但对我来说看起来不错。如果您刚刚开始使用 Scala,我相信它很快就会开始感觉良好 - 当有更多惯用的替代方案时,避开 Java 式的做事方式很重要。

回答by agilesteel

For what it's worth you can introduce an extra scope to "mark" the init code.

对于值得的,您可以引入一个额外的范围来“标记”初始化代码。

class Foo {
   {
      // init code here
   }
}

回答by Daniel C. Sobral

The init code isthe body of the method. But you can do this, if it bothers you all that much:

init 代码方法的主体。但是你可以这样做,如果它让你如此困扰:

class Foo {
        locally { 
            //some init code
        }
}

回答by Dan Simon

Well putting the init code in the class body is the only way to have a constructor without parameters. I suppose if you want you could do something like :

好吧,将 init 代码放在类主体中是拥有无参数构造函数的唯一方法。我想如果你愿意,你可以做这样的事情:

class Foo {

  private def init {
    //init code here
  }

  init()
}

that's as close as you're gonna get.

那是你要得到的最接近的。