Kotlin泛型 [英] Kotlin generics
本文介绍了Kotlin泛型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Kotlin中如何为一个方法强制执行泛型?
我知道你可以做以下事情:
How is it possible to enforce a generic type for a method in Kotlin? I know for instance you can do the following:
var someVar: MutableSet<out SomeType> = hashSetOf()
如何为一个方法做同样的事情?
How can you do the same for a method?
fun <T> doSomething() {
}
我想执行 T
类型为 X
或其子类型。
I'd like to enforce T
to be of type X
or a sub-type of it.
谢谢。
推荐答案
在google搜索后,正确的答案是:
After googling around, the correct answer would be:
fun <T : X> doSomething() {
}
这篇关于Kotlin泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文