【发布时间】:2021-03-25 10:31:10
【问题描述】:
我正在 Android 上使用 ViewBindings,编译器从 xml 生成的代码必须看起来像这样
class ViewBinding {...}
class MyBinding : ViewBinding {
companion object {
fun inflate (...) {
...
}
}
}
为避免复制/粘贴代码,我想创建一个类,接受 ViewBinding 的子类作为泛型类型参数,并使用具有方法 inflate 的伴随对象。
class MyClass<T:ViewBinding having the method inflate in the companion object of the class> { ... }
最聪明的方法是什么?
【问题讨论】:
-
需要更多关于你想要做什么的细节。什么是 MyClass?您的 ViewBinding 的子类,您的 ViewBinding 的某种包装器,还是别的什么?
标签: android kotlin generics inheritance companion-object