第九章 Scala继承

1:在Scala中 
      val修饰的抽象类的字段只能使用val覆盖
      var修饰的抽象类的字段只能使用var覆盖
      def 修饰的无参函数可以使用var或者def覆盖
abstract  class scalajicheng {
   val a:Int
   var s:String
   def add:Int
}
class test extends scalajicheng{
    override   val a=3     //val抽象类字段只能使用val覆盖
      var s="111"  //var抽象类字段只能使用var覆盖
    var add=1     //def无参数函数可以使用var或者def覆盖
}
 
这里的override  可以写也可以不写
只有是继承一般类的时候 才必须写来覆盖方法
 
class scalajich {
   val a:Int=1
   var s:String=""
   def add:Int=1
}
 
class test extends scalajich{
  override val a=3     //val抽象类字段只能使用val覆盖
 //override var s="111"  //一般类不能这样重新不然会报错
  override def add=1     //如果继承的是一般类这个就要用 def修饰了
}
 
 
 
posted @ 2017-12-26 21:04  瀚海行舟  阅读(97)  评论(0编辑  收藏  举报