语法:多个通用约束和继承、接口
syntax: multiple generic constraints and inheritance, interfaces
在 Kotlin 中指定多个泛型 bounds/constraints 的正确语法是什么?
class CustomClass<T> where T: Constraint1, T: Constraint2,
ParentClass<T>(), Interface1 { /* ... */ }
此处 Constraint1
和 Constraint2
在 T
上不相关 constraints/bounds(例如:T
实现的不相交接口)并且 ParentClass
是通用(基础)class 也。 Interface1
是一个接口 CustomClass
将满足
您需要在 where
子句之前指定基础 class 和接口:
class CustomClass<T>
: ParentClass<T>(), Interface1
where T : Constraint1, T : Constraint2 {
/* ... */
}
在 Kotlin 中指定多个泛型 bounds/constraints 的正确语法是什么?
class CustomClass<T> where T: Constraint1, T: Constraint2,
ParentClass<T>(), Interface1 { /* ... */ }
此处 Constraint1
和 Constraint2
在 T
上不相关 constraints/bounds(例如:T
实现的不相交接口)并且 ParentClass
是通用(基础)class 也。 Interface1
是一个接口 CustomClass
将满足
您需要在 where
子句之前指定基础 class 和接口:
class CustomClass<T>
: ParentClass<T>(), Interface1
where T : Constraint1, T : Constraint2 {
/* ... */
}