在kotlin中,如何从Parcelable父对象(并实现了嵌套的Builder类)中派生出一个类? [英] In kotlin how to make a derived class from a Parcelable parent (and has implemented a nested Builder class)?

查看:662
本文介绍了在kotlin中,如何从Parcelable父对象(并实现了嵌套的Builder类)中派生出一个类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Having a base class (which is Parceable) using Builder pattern, now would like to create a child class derived from it so that override the default customFunc_1 and customFunc_2 function implementation.

如果只是从基类派生的话,

If simply deriving from the base class,

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,

after put in the bundle and getParcelbale from the bundle,

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

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

it cast back to the base class (lost the overridden function implementation from the derived class)

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

如果没有办法重用基类的生成器,那是可以的,但是似乎很难从Parcelable的父类派生.

If there is no way to reuse the base class's builder it is ok, but seems having problem to derive from a parent which is Parcelable.

有人知道该怎么做吗?

Anyone knows how to do it?

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)

推荐答案

Find the solution for derive from a Parcelable parent class at here copy here for ref:

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类.还是有人有更好的方法?

And I guess for the Builder it may have to add a nested Builder class in the derived class. Or anyone has better way?

这篇关于在kotlin中,如何从Parcelable父对象(并实现了嵌套的Builder类)中派生出一个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆