简体   繁体   中英

Scala: abstract classes instantiation with trait?

This a example code in the book:

abstract class Check {
  def check():String = "Checked Application Details... "
}
trait EmploymentCheck extends Check{
  override def check():String = "Check Employment ... " + super.check()
}

val app = new Check with EmploymentCheck

Which make me confused is the new Check , How can we instantiation a abstract class ? And why it will work by with EmploymentCheck ?

new Check with EmploymentCheck generates an anonymous concrete subclass of Check . No abstract class is instantiated.

scala> app.getClass
res0: Class[_ <: EmploymentCheck] = class $anon$1

您要实例化的类型是Check with EmploymentCheck不是Check ,并且Check with EmploymentCheck不是抽象的,因为抽象成员的check已经由EmploymentCheck填写。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM