classPoint(val xc: Int, val yc: Int) { var x: Int = xc var y: Int = yc defmove(dx: Int, dy: Int) { x = x + dx y = y + dy println ("x 的坐标点 : " + x); println ("y 的坐标点 : " + y); } }
classLocation(override val xc: Int, override val yc: Int,val zc :Int) extendsPoint(xc, yc){ var z: Int = zc
defmove(dx: Int, dy: Int, dz: Int) { x = x + dx y = y + dy z = z + dz println ("x 的坐标点 : " + x); println ("y 的坐标点 : " + y); println ("z 的坐标点 : " + z); } }
val loc = newLocation(10, 20, 15);
// 移到一个新的位置 loc.move(10, 10, 5);
x 的坐标点 : 20
y 的坐标点 : 30
z 的坐标点 : 20
Scala重写一个非抽象方法,必须用override修饰符。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classPerson{ var name = "" overridedeftoString= getClass.getName + "[name=" + name + "]" }
classPoint(xc: Int, yc: Int) extendsEqual{ var x: Int = xc var y: Int = yc defisEqual(obj: Any) = obj.isInstanceOf[Point] && obj.asInstanceOf[Point].x == x }
val p1 = newPoint(2, 3) val p2 = newPoint(2, 4) val p3 = newPoint(3, 3)