Scala抽象类不能被实例化,包含若干定义不完全的方法,具体的实现由子类去实现。
1、代码示例
package com.yy.oop
//抽象类
abstract class Animal1{
//抽象字段
var name:String
var size:Int
//抽象方法
def walk
}
//抽象类实现类
class Cat(var length:Int)extends Animal1{
override var name = "cat"
override var size = 100
override def walk{
println(this.name + ":" + this.size + ":" + this.length)
}
}
object AbstractClassTest {
def main(args: Array[String]): Unit = {
val cat = new Cat(200)
cat.walk
println("name:" + cat.name)
println("size:" + cat.size)
println("length:" + cat.length)
}
}
2、结果如下
cat:100:200
name:cat
size:100
length:200