`this`输入Scala
发布时间:2020-12-16 18:34:12 所属栏目:安全 来源:网络整理
导读:看看这个问题,Fill immutable map with for loop upon creation,我很想知道这在Map中是什么意思(1 – 这个). scala Map(1 - this)res6: scala.collection.immutable.Map[Int,type] = Map(1 - @53e28097)scala res6(1)res7: type = @53e28097 我以前没见过类
看看这个问题,Fill immutable map with for loop upon creation,我很想知道这在Map中是什么意思(1 – >这个).
scala> Map(1 -> this) res6: scala.collection.immutable.Map[Int,type] = Map(1 -> @53e28097) scala> res6(1) res7: type = @53e28097 我以前没见过类型作为类型. 它是什么? 解决方法
它似乎在REPL中有点奇怪,但如果您实际编译或解释脚本,这确实似乎指向封闭对象的当前实例.
import scala.reflect.runtime.{ universe => ru } object Main { def getType[T : ru.TypeTag](instance: T) = ru.typeOf[T] def sayHello = println("hello!") def main(args: Array[String]): Unit = { println(this.getType(123)) // Prints "Int" this.sayHello // Prints "hello!" to the console getType(this).decls foreach println _ // Prints the following outputs to the console: // constructor Main // method getType // method sayHello // method main } } 至于为什么它在REPL中没有表现出这种行为,我不确定. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容