scala:抽象类实例化? [英] scala: abstract classes instantiation?

查看:264
本文介绍了scala:抽象类实例化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何实例化一个抽象类?

How it comes that I instantiate an abstract class?

  abstract class A {
    val a: Int
  }

  val a = new A {val a = 3}

还是隐式创建了一些具体的类? 新A 之后的括号是什么意思?

Or is some concrete class implicitly created? And what do those braces after new A mean?

推荐答案

,则隐式扩展 A 。您所做的是语法糖,等效于此:

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'

这些括号仅允许您扩展a类,创建一个新的匿名类,该类实例化值 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.

这篇关于scala:抽象类实例化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆