首页 > 代码库 > Scala学习笔记

Scala学习笔记

1 闭包

     var a = 3     var addfun = (x:Int) => x + a     var b = addfun(10)     println(b)

  

addfun 就是个和对象无关的函数,局部变量,使用方法和函数一样,但是好像不推荐这种写法。


2 类的创建,简单使用
abstract class BParent{  def testoverride()}// 构造函数参数会自动生成类字段,如果不加val则为私有字段, 类外无法访问class B( val name : String, val age:Int) extends  BParent{  var namebak:String=_ // 初始化  val cname = "classname is B"  // 附属构造器, C++ 重载构造函数  def this(name: String, age: Int, namebak: String){    this(name, age)    println("call this " + name)    this.namebak = namebak  }  def testoverride(): Unit ={    println("my class name is B")  }}class BSon(name: String, age: Int, addr: String) extends B(name, age){  println("Bson is Create")  override val cname = "classname is BSon"  override def toString = "BSon toString "  + super.toString  override  def testoverride(): Unit ={    println("my class name is BSon")  }}

  

  调用

val b = new B("test", 32, "dd")
println(b.name)
println(b.namebak)
println(b.toString)

var bson = new BSon("bsonname", 1, "bsonaddr")
println(bson.toString)
println(bson.cname)
bson.testoverride()

 

4  

  

Scala学习笔记