提示
scala也只能支持单继承,实现多继承可以用特质
demo
//父类
class Point(val xc:Int,val yc:Int) {
var x:Int = xc
var y:Int = yc
def move(dx:Int,dy:Int):Unit = {
x+=dx
y+=dy
println("移动后x坐标为"+ this.x)
println("移动后y坐标为"+ this.y)
}
}
//定义子类,如果子类和父类定义了同名参数,需要override关键字
class Location(override val xc:Int,override val yc:Int,var z:Int) extends Point(xc,yc){
def move(dx: Int, dy: Int , dz:Int): Unit = {
super.move(dx,dy)
z += dz
println("移动后z坐标为"+this.z)
}
}
//定义主方法
object testClass {
def main(args: Array[String]): Unit = {
val obj = new Location(1,1,1)
obj.move(1,1,1)
//判断对象是不是属于给定的类
println(obj.isInstanceOf[Point])
//类型转换
obj.asInstanceOf[Point]
//获取类的信息
println(classOf[Location])
}
}