【发布时间】:2019-08-04 03:02:46
【问题描述】:
如何获取 Generic 类的类型并将对象转换为它?
我想用这个函数来传递一个接口类:
protected fun <T> getInteraction(): T {
return when {
context is T -> context
parentFragment is T -> parentFragment as T
else -> throw IllegalArgumentException("not implemented interaction")
}
}
并像这样使用它:
private var interaction: ISigninInteraction? = null
override fun onAttach(context: Context?) {
super.onAttach(context)
interaction = getInteraction<ISigninInteraction>()
}
【问题讨论】:
标签: android generics kotlin interface fragment