比方說,我有相同的構造如何在不同的類中共享相同的構造函數,即在接口中定義構造函數?
class Button: AppCompatButton {
constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {
}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
}
constructor(context: Context) : super(context) {
}
//Some custom implementation
//............
}
class TextView: AppCompatTextView {
constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {
}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
}
constructor(context: Context) : super(context) {
}
//Some custom implementation
//............
}
所以我需要一些接口或基類,它讓我繼承像TextView的,按鈕的EditText等
形式的多個視圖中的Android多個自定義視圖喜歡的東西
abstract class BaseView<T : View> : T {
constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {
}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
}
constructor(context: Context) : super(context) {
}
}
或者
interface ViewConstructor {
constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {
}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
}
constructor(context: Context) : super(context) {
}
}
所以我只使用一個接口或基類,不要複製過去的承包商。如何在Kotlin中實現這樣的目標?
P.S.請不要建議使用基類作爲View並使用基類創建派生視圖。我使用XML,我需要EditText,Button和其他視圖。