scala: abstract classes instantiation?

后端 未结 4 538
灰色年华
灰色年华 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 15:56

    If you know Java, this is similar to:

    new SomeAbstractClass() {
        // possible necessary implementation
    }
    

    Due to Scalas uniform access it looks like you're not implementing any abstract functions, but just by giving a a value, you actually do "concretesize" the class.

    In other words, you're creating an instance of a concrete subclass of A without giving the subclass a name (thus the term "anonymous" class).

提交回复
热议问题