设为首页 加入收藏

TOP

Scala--继承(一)
2017-10-10 12:11:03 】 浏览:4047
Tags:Scala-- 继承

一、扩展类

  class Persion{
    def display(){
      println("Hello")
    }
    final def display1(){   //声明为final, 不能被重写 override
      println("Hello1")
    }
  }

  class Employee extends Persion{ //extends 扩展,继承Persion
    override def display() {   //override重写 display方法
      println("H")
    }
  }

  val p = new Employee
  p.display()
  p.display1()

 

二、重写方法

  class Persion{
    private var str = ""
    def display(){
      println("Hello")
    }
    final def display1(){   //声明为final, 不能被重写 override
      println("Hello1")
    }
    def info_= (newStr:String){  //info setter方法
      str = newStr
    }
    def info = str     //info getter方法
  }

  class Employee extends Persion{ //extends 扩展,继承Persion
    override def display() {   //override重写 display方法
      println(super.info +" H")
    }
  }

  val p = new Employee
  p.info="hello"
  p.display()
  p.display1()

调用超类的方法使用: super

 

三、类型检查和转换

  class Persion{
  }

  val p = new Persion
  if(p.isInstanceOf[Persion]){  //类型检查
    println("p is instance of Persion")
  }
  else{
    println("p is not instance of Persion")
  }

  if(p.getClass == classOf[Persion]){ //p是 Persion对象,并不是Persion的子类,这样检查
    println("Yes")
  }

 

四、受保护字段和方法

protected 可以被子类访问

 

五、超类的构造

传递到超类的构造函数

  class Employee(name: String, age: Int, val salary: Double) extends
   Persion(name, age)

Scala类可以扩展java

  class Square(x:Int , y: Int, width: Int) extends
  java.awt.Rectangle(x, y, width, width)

 

六、重写字段

  class Persion(val name: String){
    override def toString = getClass.getName()+ "[name="+name+"]"
  }


  class SecretAgent (codename: String) extends Persion(codename){
    override val name = "secret"   //重写 name
    override val toString ="secret" //重写 toString
  }

  val p = new SecretAgent("hello")
  println(p.name)
  println(p.toString)

 

常用做法:用val重写抽象的def

  abstract class Persion{
    def id :Int = 10
  }

  class Student(override val id:Int) extends Persion{
  }

  val p = new Student(4)
  println(p.id)

 

七、匿名字段

  class Persion(val name:String){
  }

  val alien = new Persion("Fred"){  //匿名子类   类型为 Persion{def greeting: String}
    def greeting = "Hi everybody"
  }

  def meet(p:Persion{def greeting: String}){
    println(p.name + " says:" + p.greeting)
  }

  meet(alien)

结果:

Fred says:Hi everybody

 

八、抽象类

  abstract class Persion(val name:String){ //抽象类
    def id:Int   //方法不用定义为抽象
  }

  class Student(name:String) extends Persion(name){ //继承抽象类
    def id = name.hashCode  //实现抽象类中的方法, 不需要override关键字
  }
  
  val s = new Student("Jim")
  println(s.id)

 

九、抽象字段

  abstract class Persion{
    val id :Int
    var name : String
  }

  class Student(val id :Int) extends Persion{
    var name =""
  }

  val fred = new Persion{
    val id = 12
    var name = "Fred"
  }

  println(fred.id)
  println(fred.name)

结果:

12
Fred

 

十、构造顺序和提前定义

  class Creature {
    val range: Int = 10
    val env: Array[Int] = new Array[Int](range)
  }

  class Ant extends Creature{
    override val range: Int = 2
  }

  val c = new Ant
  println(c.range)              //结果为2
  println(c.env.length)      //结果为0

  class Creature {
    lazy val range: Int = 10    //使用lazy
    val env: Array[Int] = new Array[Int](range)
  }

  class Ant extends Creature{
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Scala--数组相关操作 下一篇Scala--类

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目