scala: abstract classes instantiation?

后端 未结 4 541
灰色年华
灰色年华 2021-02-04 15:46

How it comes that I instantiate an abstract class?

  abstract class A {
    val a: Int
  }

  val a = new A {val a = 3}

Or is some concrete cla

4条回答
  •  不思量自难忘°
    2021-02-04 16:10

    With this, you implicitly extend A. What you did is syntactic sugar, equivalent to this:

    class A' extends A {
        val a = 3
    }
    val a = new A'
    

    These brackets merely let you extend a class on the fly, creating a new, anonymous class, which instantiates the value a and is therefore not abstract any more.

提交回复
热议问题