Private and protected constructor in Scala

后端 未结 2 2059
一个人的身影
一个人的身影 2020-12-07 14:42

I\'ve been curious about the impact of not having an explicit primary constructor in Scala, just the contents of the class body.

In particular, I suspect that the pr

2条回答
  •  臣服心动
    2020-12-07 14:50

    Aleksander's answer is correct, but Programming in Scala offers an additional alternative:

    sealed trait Foo {
     // interface
    }
    
    object Foo {
      def apply(...): Foo = // public constructor
    
      private class FooImpl(...) extends Foo { ... } // real class
    }
    

提交回复
热议问题