首页 > 其他 > 详细

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

时间:2015-08-02 16:28:18      阅读:219      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!