Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在不同的类之间共享相同的构造函数,或者在接口中定义构造函数?_Android_Kotlin - Fatal编程技术网

Android 如何在不同的类之间共享相同的构造函数,或者在接口中定义构造函数?

Android 如何在不同的类之间共享相同的构造函数,或者在接口中定义构造函数?,android,kotlin,Android,Kotlin,假设我在Android中有多个具有相同构造函数的自定义视图 class Button: AppCompatButton { constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) { } constructor(context: Context, attrs: AttributeSet) : super(context, at

假设我在Android中有多个具有相同构造函数的自定义视图

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、Button、EditText等

差不多

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) {
    }
}
所以我只使用一个接口或基类,不会一次又一次地复制过去的承包商。如何在Kotlin中实现这样的目标


注意:请不要建议使用基类作为视图,并使用基类创建派生视图。我使用的是XML,需要编辑文本、按钮和其他视图。

您不太可能找到问题的解决方案。首先,构造函数不是按照您建议的方式继承的。它们的存在是为了初始化继承链的一部分,因此从您继承的任何人仍然需要转发到超级类构造函数。我敢肯定,您在重新定义每个视图类中的视图构造函数时遇到了困难(尽管根据实例化用例,您可能会删除其中的一些构造函数)。

编辑:我刚刚偶然发现了这个答案,它需要更新。在某些自定义视图中,使用
@JvmOverloads
可能是一个问题,因为调用为自己的类生成的任何构造函数都将首先委托给类中的all params构造函数,然后调用super的all params构造函数,而不是每个构造函数调用具有匹配数量的参数的超级方法。有关这方面的详细信息,请参见(例如)

我的答案原文如下


作为必须编写这么多构造函数的解决方案,您可以将默认参数与
@JvmOverloads
结合使用,轻松获取所有4个
视图
构造函数,同时只为类编写主构造函数:

class CustomView @JvmOverloads constructor(
        context: Context,
        attrs: AttributeSet? = null,
        defStyleAttr: Int = 0,
        defStyleRes: Int = 0
) : View(context, attrs, defStyleAttr, defStyleRes) {

}
class CustomView @JvmOverloads constructor(
        context: Context,
        attrs: AttributeSet? = null,
        defStyleAttr: Int = 0,
        defStyleRes: Int = 0
) : View(context, attrs, defStyleAttr, defStyleRes) {

}