简体   繁体   English

没有参数的Scala构造函数

[英]Scala constructor without parameters

I may be having a silly problem here... I can't seem to figure out how to make a constructor without parameters in Scala. 我这里可能有一个愚蠢的问题...我似乎无法弄清楚如何在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. 我知道我可以将整个事情写在类主体中(特别是因为它是我唯一需要的构造函数),但是感觉不太对。

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
        }

        //...
    }

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 always secondary 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. FWIW,第一个示例是向您开放的唯一选择,但对我来说很好。 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. 如果您刚刚开始使用Scala,我相信它很快就会开始流行起来-当有更多惯用的替代方法时,避免使用Java风格的做事方式非常重要。

For what it's worth you can introduce an extra scope to "mark" the init code. 为此,您可以引入一个额外的作用域来“标记”初始化代码。

class Foo {
   {
      // init code here
   }
}

The init code is the body of the method. 初始化代码方法的主体。 But you can do this, if it bothers you all that much: 但您可以这样做,如果它使您非常困扰:

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

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 : 我想如果您愿意,您可以执行以下操作:

class Foo {

  private def init {
    //init code here
  }

  init()
}

that's as close as you're gonna get. 距离您将近。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM