标签:关键字 ati 属性 getc color ext 字段 抽象 抽象方法
Scala继承一个基类跟Java很相似, 但我们需要注意以下几点:
1 class Point(val xc: Int, val yc: Int){ 2 var x: Int = xc 3 var y: Int = yc 4 def move(dx: Int, dy: Int): Unit ={ 5 x = x + dx 6 y = y + dy 7 println("x点的坐标是:" + x) 8 println("y点的坐标是:" + y) 9 } 10 11 var name = "" 12 override def toString = getClass.getName + "[name=" + name + "]" 13 }
1 class Location(override val xc: Int, override val yc: Int, 2 val zc: Int) extends Point(xc, yc){ // 继承 重写了父类的字段 3 var z: Int = zc 4 def move(dx: Int, dy: Int, dz: Int){ 5 x = x + dx 6 y = y + dy 7 z = z + dz 8 println("x点的坐标是:" + x) 9 println("y点的坐标是:" + y) 10 println("z点的坐标是:" + z) 11 } 12 13 var salary = 0.0 14 override def toString = super.toString + "[salary=" + salary + "]" 15 }
1 object Test{ 2 def main(args: Array[String]): Unit = { 3 val loc = new Location(10, 20, 30) 4 loc.move(10, 10 ,5)、 6 loc.name = "lc" 7 loc.salary = 35000.0 8 println(loc) 9 } 10 }
标签:关键字 ati 属性 getc color ext 字段 抽象 抽象方法
原文地址:https://www.cnblogs.com/9981-lyq/p/10434781.html