码迷,mamicode.com
首页 > 其他好文 > 详细

Scala继承中val变量的构造顺序

时间:2015-08-02 16:28:59      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:

例子1

class A {
  val x1: String = "hello"
  val x2: String = "mom"
  println("A: x1=" + x1 + ",x2=" + x2)
}

class B extends A {
  override val x2: String = "dad"
  println("B: x1=" + x1 + ",x2=" + x2)
}

object ValTest extends App {
  new A
  println()
  new B
}

输出:

A: x1=hello,x2=mom

A: x1=hello,x2=null
B: x1=hello,x2=dad

 

例子2:

class A {
  val x1: String = "hello"
  val x2: String = x2
  println("A: x1=" + x1 + ",x2=" + x2)
}

class B extends A {
  override val x1: String = "dad"
  println("B: x1=" + x1 + ",x2=" + x2)
}

object ValTest extends App {
  new A
  println()
  new B
}

输出:

A: x1=hello,x2=null

A: x1=null,x2=null
B: x1=dad,x2=null

 

参考 http://blog.iamzsx.me/show.html?id=674002 

Scala继承中val变量的构造顺序

标签:

原文地址:http://www.cnblogs.com/Murcielago/p/4695906.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!