在Kotlin中,如何从Parcelable父对象(并实现了嵌套的Builder类)中派生类?

时间:2019-02-10 21:19:18

标签: android inheritance kotlin parcelable builder

具有使用Builder模式的基类(可Parceable),现在想创建一个从其派生的子类,以便覆盖默认的customFunc_1customFunc_2函数实现。

如果只是从基类派生的,

class DerivedDataConfig : BaseDataConfig {
    override open fun customFunc_1(context: Context, savedInstanceState: Bundle?,
                                   onElementClickListener: ElementClickListener? = null) : FrameLayout? {
        // differnt than base
        Log.i("+++", "+++, customFunc_1 called in derived class")
        return android.widget.FrameLayout(context)
    }

    override fun customFunc_2(viewToBind: View, content: IData, position: Int) {
        Log.i("+++", "+++, customFunc_2 called in derived class")
    }
}  

放入捆绑包并从捆绑包中获取getParcelbale,

bundle.putParcelable(KEY_DATA_CONFIG, derivedDataConfig)
var derivedDataConfig.getParcelable(KEY_DATA_CONFIG)

它强制返回基类(从派生类中丢失了重写的函数实现)

如何在kotlin中做到从可适应的基类派生?

如果没有办法重用基类的生成器,那没关系,但似乎很难从Parcelable的父类派生。

有人知道怎么做吗?

open class BaseDataConfig() : Parcelable {
    var param_1 = false
    var param_2 = ArrayList<DataDescriptor>()

    private constructor(parcel: Parcel) : this() {
        param_1 = parcel.readByte() != 0.toByte()
        parcel.readList(param_1, DataDescriptor::class.java.classLoader)
    }

    open fun customFunc_1(context: Context, savedInstanceState: Bundle?,
                          onElementClickListener: ElementClickListener? = null) : FrameLayout? {
        return null
    }

    open fun customFunc_2(viewToBind: View, content: IData, position: Int) {
    }

    class Builder {
        private var param_1 = false
        private var param_2 = ArrayList<DataDescriptor>()
        fun setParam_1(b: Boolean) = apply { this.param_1 = b }
        fun setParam_2(type: String, id: Int) = apply { this.param_2.add(DataDescriptor(type, id)) }
        fun build() : DataConfig {
            return DataConfig().also {
                it.param_1 = param_1
                it.param_1_2 = param_2
            }
        }
    }

    override fun writeToParcel(dest: Parcel, flags: Int) {
        dest.writeByte(if (param_1) 1 else 0)
        dest.writeList(param_2)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object {

        @JvmField
        val CREATOR: Parcelable.Creator<DataConfig> = object : Parcelable.Creator<DataConfig> {
            override fun createFromParcel(parcel: Parcel): DataConfig {
                return DataConfig(parcel)
            }

            override fun newArray(size: Int): Array<DataConfig?> {
                return arrayOfNulls(size)
            }
        }
    }

}

class DataDescriptor(val type: String, val id: Int)

1 个答案:

答案 0 :(得分:0)

here查找从Parcelable父类派生的解决方案 复制此处以供参考:

class SavedState extends BaseSavedState {
    int stateToSave;

    SavedState(Parcelable superState) {
      super(superState);
    }

    private SavedState(Parcel in) {
      super(in);
      this.stateToSave = in.readInt();
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
      super.writeToParcel(out, flags);
      out.writeInt(this.stateToSave);
    }

    //required field that makes Parcelables from a Parcel
    public static final Parcelable.Creator<SavedState> CREATOR =
        new Parcelable.Creator<SavedState>() {
          public SavedState createFromParcel(Parcel in) {
            return new SavedState(in);
          }
          public SavedState[] newArray(int size) {
            return new SavedState[size];
          }
    };
}

我想对于Builder,可能必须在派生类中添加一个嵌套的Builder类。还是有人有更好的方法?