scala强制类型转换
所属分类 scala
浏览量 679
package demo.cast
class Pet() {
var name: String = "cat"
override def toString: String = name
}
object CastDemo {
def main(args: Array[String]) {
val pet: Any = new Pet()
val pet2 = pet.asInstanceOf[Pet]
val pet3 = Class.forName("demo.cast.Pet").newInstance().asInstanceOf[Pet]
val petobj = Class.forName("demo.cast.Pet").newInstance()
val pet4 = classOf[Pet].cast(petobj)
pet4.name = "cat2"
println(pet)
println(pet2)
println(pet3)
println(petobj)
println(pet4)
}
}
上一篇
下一篇
scala akka remote 实例
scala akka remote ask 实例
scala future mapTo 用法
scala成员访问修饰符
protobuf 简介 及 java实例
scala包对象实例