开发者

Type from Scala reflection

开发者 https://www.devze.com 2023-04-11 04:29 出处:网络
Suppose that I have: trait A class B extends A compiled into class files. Later I load those using 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

精彩评论

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

关注公众号