通用扩展类并在 Kotlin 中实现接口 [英] Generic extending class AND implements interface in Kotlin
本文介绍了通用扩展类并在 Kotlin 中实现接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我想要一个类型变量 T,它扩展某个类并实现一个接口.类似的东西:
Say I want a type variable, T, that extends a certain class and implements an interface. Something like:
class Foo <T : Bar implements Baz> { ... }
这在 Kotlin 中的语法是什么?
What is the syntax for this in Kotlin?
推荐答案
尖括号内只能指定一个上限.
Only one upper bound can be specified inside the angle brackets.
当存在多个约束时,Kotlin 为通用约束提供了不同的语法:
Kotlin offers different syntax for generic constraints when there is more than one constraint:
class Foo<T>(val t: T) where T : Bar, T : Baz { ... }
和函数:
fun <T> f(): Foo where T : Bar, T : Baz { ... }
记录在此处.
这篇关于通用扩展类并在 Kotlin 中实现接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文