开发者

Type from Scala reflection

Suppose that I have:

trait A

class B extends A

compiled into class files. Later I load those using reflection:

val a = Class forName "A"
val b = Class forName "B"

Could anyone tell me h开发者_StackOverflow中文版ow to check whether b is the subtype of a?


Use the isAssignableFrom method in Class:

a isAssignableFrom b

This returns true if b is a subclass/subinterface of a or b == a.


You can just call the getInterfaces method on b and iterate through the array to see if any of them equals a.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜