子类签名中的超级构造函数?

时间:2012-09-27 10:35:48

标签: scala subclass extend

在此页面的示例中:http://www.scala-lang.org/node/125

class Point(xc: Int, yc: Int) {
  val x: Int = xc
  val y: Int = yc
  def move(dx: Int, dy: Int): Point =
    new Point(x + dx, y + dy)
}

class ColorPoint(u: Int, v: Int, c: String) extends Point(u, v) {
  val color: String = c
  def compareWith(pt: ColorPoint): Boolean =
    (pt.x == x) && (pt.y == y) && (pt.color == color)
override def move(dx: Int, dy: Int): ColorPoint =
  new ColorPoint(x + dy, y + dy, color)
}

扩展类的参数/参数列表在子类的定义中用于什么目的?我指的是(u, v)行中Point末尾的class ColorPoint(u: Int, v: Int, c: String) extends Point(u, v) {

1 个答案:

答案 0 :(得分:3)

如果您熟悉Java,则此代码将完全相同:

class ColorPoint extends Point {
  ColorPoint (int u, int v, String c) {
    super(u,v);
  ...
  }
  ...
}

所以,是的,它是对super的构造函数的调用

相关问题