Scala 构造函数重载?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1095329/
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 01:31:11  来源:igfitidea点击:

Scala constructor overload?

scala

提问by Landon Kuhn

How do you provide overloaded constructors in Scala?

你如何在 Scala 中提供重载的构造函数?

回答by Jon McAuliffe

It's worth explicitly mentioning that Auxiliary Constructors in Scala must either call the primary constructor (as in landon9720's) answer, or another auxiliary constructor from the same class, as their first action. They cannot simply call the superclass's constructor explicitly or implicitly as they can in Java. This ensures that the primary constructor is the sole point of entry to the class.

值得明确提及的是,Scala 中的辅助构造函数必须调用主构造函数(如在landon9720 中)的答案,或者来自同一类的另一个辅助构造函数,作为它们的第一个动作。它们不能像在 Java 中那样简单地显式或隐式调用超类的构造函数。这确保主构造函数是该类的唯一入口点。

class Foo(x: Int, y: Int, z: String) {  
  // default y parameter to 0  
  def this(x: Int, z: String) = this(x, 0, z)   
  // default x & y parameters to 0
  // calls previous auxiliary constructor which calls the primary constructor  
  def this(z: String) = this(0, z);   
}

回答by Landon Kuhn

 class Foo(x: Int, y: Int) {
     def this(x: Int) = this(x, 0) // default y parameter to 0
 }

回答by J?rgen Lundberg

As of Scala 2.8.0 you can also have default values for contructor- and method parameters. Like this

从 Scala 2.8.0 开始,您还可以为构造函数和方法参数设置默认值。像这样

scala> class Foo(x:Int, y:Int = 0, z:Int=0) {                           
     | override def toString() = { "Foo(" + x + ", " + y + ", " + z + ")" }
     | }
defined class Foo

scala> new Foo(1, 2, 3)                                                    
res0: Foo = Foo(1, 2, 3)

scala> new Foo(4)                                                          
res1: Foo = Foo(4, 0, 0)

Parameters with default values must come after the ones with no default values in the parameter list.

具有默认值的参数必须在参数列表中没有默认值的参数之后。

回答by flying sheep

While looking at my code, I suddenly realized that I did kind of an overload a constructor. I then remembered that question and came back to give another answer:

在查看我的代码时,我突然意识到我做了一个构造函数的重载。然后我想起了那个问题,回来给出另一个答案:

In Scala, you can't overload constructors, but you can do this with functions.

在 Scala 中,你不能重载构造函数,但你可以用函数来做到这一点。

Also, many choose to make the applyfunction of a companion object a factory for the respective class.

此外,许多人选择将apply伴随对象的函数作为相应类的工厂。

Making this class abstract and overloading the applyfunction to implement-instantiate this class, you have your overloaded “constructor”:

使这个类抽象并重载apply函数来实现-实例化这个类,你就有了重载的“构造函数”:

abstract class Expectation[T] extends BooleanStatement {
    val expected: Seq[T]
    …
}

object Expectation {
    def apply[T](expd:     T ): Expectation[T] = new Expectation[T] {val expected = List(expd)}
    def apply[T](expd: Seq[T]): Expectation[T] = new Expectation[T] {val expected =      expd }

    def main(args: Array[String]): Unit = {
        val expectTrueness = Expectation(true)
        …
    }
}

Note that I explicitly define each applyto return Expectation[T], else it would return a duck-typed Expectation[T]{val expected: List[T]}.

请注意,我明确地将每个都定义apply为 return Expectation[T],否则它将返回一个duck-typed Expectation[T]{val expected: List[T]}

回答by asyncwait

I thought may be Scala Constructors(2008-11-11) could add more information.

我想可能是Scala Constructors(2008-11-11) 可以添加更多信息。

回答by anish

Try this

试试这个

class A(x: Int, y: Int) {
  def this(x: Int) = this(x, x)
  def this() = this(1)
  override def toString() = "x=" + x + " y=" + y
  class B(a: Int, b: Int, c: String) {
    def this(str: String) = this(x, y, str)
    override def toString() =
      "x=" + x + " y=" + y + " a=" + a + " b=" + b + " c=" + c
  }
}