Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 继承:在类构造函数中重写_Scala_Inheritance_Overriding - Fatal编程技术网

Scala 继承:在类构造函数中重写

Scala 继承:在类构造函数中重写,scala,inheritance,overriding,Scala,Inheritance,Overriding,我正在阅读此链接中的示例 示例: class Point(val xc: Int, val yc: Int) { var x: Int = xc var y: Int = yc def move(dx: Int, dy: Int) { x = x + dx y = y + dy println ("Point x location : " + x); println ("Point y locatio

我正在阅读此链接中的示例 示例:

class Point(val xc: Int, val yc: Int) {
   var x: Int = xc
   var y: Int = yc
   def move(dx: Int, dy: Int) {
      x = x + dx
      y = y + dy
      println ("Point x location : " + x);
      println ("Point y location : " + y);
   }
}

class Location(override val xc: Int, override val yc: Int,
   val zc :Int) extends Point(xc, yc){
   var z: Int = zc

   def move(dx: Int, dy: Int, dz: Int) {
      x = x + dx
      y = y + dy
      z = z + dz
      println ("Point x location : " + x);
      println ("Point y location : " + y);
      println ("Point z location : " + z);
   }
}

object Test {
   def main(args: Array[String]) {
      val loc = new Location(10, 20, 15);

      // Move to a new location
      loc.move(10, 10, 5);
   }
}
我不明白override关键字在类位置构造函数中的作用! 既然我们这里有一个扩展,为什么要提到它? 谢谢

给定

scala> class A(val a: Int)
defined class A
如果您试图这样定义
B

scala> class B(val a: Int) extends A(a)
<console>:11: error: overriding value a in class A of type Int;
 value a needs `override' modifier
       class B(val a: Int) extends A(a)
更具体地说,如果要覆盖抽象成员,则不必提供
覆盖

scala> trait A { def a: Int }
defined trait A

scala> class B(override val a: Int) extends A
defined class B

scala> class B(val a: Int) extends A
defined class B
然而,为了避免在特性中混合时出现意外的覆盖,Scala通过要求显式的
覆盖
来保护您

考虑这个例子:

这里没问题:

scala> trait A { def a: Int = 1 }
defined trait A

scala> class B
defined class B

scala> new B with A
res0: B with A = $anon$1@3e29739a
您将从模糊中保存:

scala> trait A { def a: Int = 1 }
defined trait A

scala> class B { def a: Int = 2 }
defined class B

scala> new B with A
<console>:13: error: <$anon: B with A> inherits conflicting members:
  method a in class B of type => Int  and
  method a in trait A of type => Int
(Note: this can be resolved by declaring an override in <$anon: B with A>.)
       new B with A
           ^
给定

如果您试图这样定义
B

scala> class B(val a: Int) extends A(a)
<console>:11: error: overriding value a in class A of type Int;
 value a needs `override' modifier
       class B(val a: Int) extends A(a)
更具体地说,如果要覆盖抽象成员,则不必提供
覆盖

scala> trait A { def a: Int }
defined trait A

scala> class B(override val a: Int) extends A
defined class B

scala> class B(val a: Int) extends A
defined class B
然而,为了避免在特性中混合时出现意外的覆盖,Scala通过要求显式的
覆盖
来保护您

考虑这个例子:

这里没问题:

scala> trait A { def a: Int = 1 }
defined trait A

scala> class B
defined class B

scala> new B with A
res0: B with A = $anon$1@3e29739a
您将从模糊中保存:

scala> trait A { def a: Int = 1 }
defined trait A

scala> class B { def a: Int = 2 }
defined class B

scala> new B with A
<console>:13: error: <$anon: B with A> inherits conflicting members:
  method a in class B of type => Int  and
  method a in trait A of type => Int
(Note: this can be resolved by declaring an override in <$anon: B with A>.)
       new B with A
           ^