我正在做一个扩展类的简单scala应用程序。 我收到一个错误,我不理解,即错误是位于类的“私人[this] val xc:Int”。
请在下面找到代码。
class Point(xc:Int ,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 for Point Class = "+x)
println("Point y location for Point Class = "+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 Runner
{
def main(arg : Array[String])
{
//val pt = new Point(4,5);
//pt.move(4, 5)
val lo = new Location(1,2,3);
lo.move(1, 2,3)
}
}
答案 0 :(得分:0)
可能是因为在scala中覆盖私有/受保护成员的问题
https://issues.scala-lang.org/browse/SI-1994
你可以通过这种方式声明类Point来解决这个问题:
class Point(val xc:Int , val yc:Int)
*当你不在param中写val时,他们是私人成员
答案 1 :(得分:0)
这有效:
class Point(var x: Int, var y: Int) {
def move(dx: Int, dy: Int) = {
x=x+dx
y=y+dy
}
}
class Location(
x: Int,
y: Int,
var z :Int) extends Point(x, y) {
def move(dx: Int, dy: Int, dz: Int) = {
super.move(dx, dy)
z = z + dz
}
}
object Runner {
def main(arg : Array[String]) = {
val pt = new Point(4,5)
pt.move(4, 5)
val lo = new Location(1,2,3)
lo.move(1, 2,3)
}
}