kotlin-android java.io.Serializable抽象类未序列化

时间:2019-07-04 16:33:07

标签: android android-intent serialization kotlin

有人知道为什么这个类层次结构序列化会导致一个空白对象吗?

abstract class Notation<T>: Comparable<T> where T: Serializable, T: Cloneable {
    var id: Long = 0L
    var aggregationId: String = ""
    var startTime: Calendar = GregorianCalendar.getInstance().apply { clear() }
    var endTime: Calendar = GregorianCalendar.getInstance().apply { clear() }
    override fun toString(): String
            = "\tid: $id,\n" +
            "\taggregationId: $aggregationId,\n" +
            "\tstartTime: $startTime,\n" +
            "\tendTime: $endTime"
}

abstract class LaborNotation<T>: Notation<T>(), Comparable<T> where T: Serializable, T: Cloneable {
    var laborId: Long = 0L
    var name: String = ""
    override fun toString(): String
            = "${super.toString()},\n" +
            "\tlaborId: $laborId,\n" +
            "\tname: $name"
}

class ComponentCrewNotation: LaborNotation<ComponentCrewNotation>(), Serializable, Cloneable {
    override fun compareTo(other: ComponentCrewNotation): Int = toString().compareTo(other.toString())
    override fun toString(): String = "{\n${super.toString()}\n}"

    public override fun clone(): Any {
        return super.clone()
    }
}

当我尝试将ComponentCrewNotation中的Intent::extra传递给其他活动时,当我调用Intent::getSerializableExtra()时,它会清除ComponentCrewNotation

为解决此问题,我已完成以下变通方法:$

class ComponentCrewNotation: LaborNotation<ComponentCrewNotation>(), Serializable, Cloneable {
    override var id: Long = super.id
    override var aggregationId: String = super.aggregationId
    override var startTime: Calendar = super.startTime
    override var endTime: Calendar = super.endTime
    override var laborId: Long = super.laborId
    override var name: String = super.name
    override fun compareTo(other: ComponentCrewNotation): Int = toString().compareTo(other.toString())
    override fun toString(): String = "{\n${super.toString()}\n}"

    public override fun clone(): Any {
        return super.clone()
    }
}

有一种更好的方法来序列化此对象,而不必覆盖每个字段?

谢谢!

0 个答案:

没有答案