开发者

Converting Types

开发者 https://www.devze.com 2023-01-15 07:43 出处:网络
I was trying to convert an object with Object type into FontUIResource type. In Java, it would be FontUIResource font开发者_如何学Python = (FontUIResource)value

I was trying to convert an object with Object type into FontUIResource type. In Java, it would be

FontUIResource font开发者_如何学Python = (FontUIResource)value

How would I do that in Scala?


You can say value.asInstanceOf[FontUIResource], or you can use a match-case block:

value match{
  case f:FontUIResource => 
    //do something with f, which is safely cast as a FontUIResource
  case _ => 
    //handle the case when it's not the desired type
}


You mean casting, not Boxing and Unboxing, since that applies to primitive values. value.asInstanceOf[FountUIResource] is the way to cast this in Scala.

0

精彩评论

暂无评论...
验证码 换一张
取 消