Scala constructor overload?

前端 未结 5 1330
栀梦
栀梦 2020-11-27 15:27

How do you provide overloaded constructors in Scala?

相关标签:
5条回答
  • 2020-11-27 15:29

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

    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.

    0 讨论(0)
  • 2020-11-27 15:38
     class Foo(x: Int, y: Int) {
         def this(x: Int) = this(x, 0) // default y parameter to 0
     }
    
    0 讨论(0)
  • 2020-11-27 15:41

    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.

    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);   
    }
    
    0 讨论(0)
  • 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
      }
    }
    
    0 讨论(0)
  • 2020-11-27 15:53

    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.

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

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

    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 apply to return Expectation[T], else it would return a duck-typed Expectation[T]{val expected: List[T]}.

    0 讨论(0)
提交回复
热议问题