Initializing a val lately

前端 未结 3 1556
旧时难觅i
旧时难觅i 2021-01-23 17:51

Is it possible to do that in Scala using only val:

class MyClass {
  private val myVal1: MyClass2 //.....????? what should be here?

  def myMethod1(param1: Int)         


        
3条回答
  •  遇见更好的自我
    2021-01-23 18:48

    No, it isn't possible to do in the way you want. Consider, what would be the result of

    val mc = new MyClass
    mc.method1(0)
    mc.method1(1)
    

    ? An exception thrown for setting myVal1 twice? Or should it keep the first value?

提交回复
热议问题