scala中的apply和update方法

来源:互联网 发布:php mysql防止sql注入 编辑:程序博客网 时间:2024/05/16 05:30

scala中的apply方法的隐式调用方法为:

对象(参数)


scala中的update方法的隐式调用方法为:

对象(参数1) = 参数2


示例如下:


class MyClass{
  def apply(str:String):Unit={
    println("applying.." + str)
  }


  def update(str1:String,str2:String):Unit={
    println("update.." + str1 + str2)
  }
}


object  MyClass{
  def main(args: Array[String]): Unit = {
    val mine = new MyClass
    mine("hello")
    mine("world!")


    mine("hello") = "world"
  }


}


把以上内容保存到文件

applyUpdate.scala 



在linux终端运行命令:

scalac  applyUpdate.scala 

scala MyClass


输出如下:

applying..hello

applying..world!

update..helloworld



windows使用cmd黑窗口,或者使用IDE直接run即可。


0 0
原创粉丝点击