我有Category实现的类Parcelable,我还有更多的类从Category类扩展.我的基类有2个protected成员title,id主要来自继承的类.因此,为了不在Parcelable继承的类中的任何地方实现相关的东西,我决定在基类中执行它并让它处理所有操作.
问题是我不能拥有Category类的构造函数,因为它是抽象类.那么解决方案是什么?由于我在类中有抽象方法,因此无法删除抽象修饰符.
public abstract class Category implements Parcelable {
private static Map<Integer, Category> categoryMap = new TreeMap<Integer, Category>();
protected Sting title;
protected Integer id;
static {
categoryMap.put(0, new Taxi());
categoryMap.put(1, new Hotel());
}
private Category(Parcel in) {
this.id = in.readInt();
this.title = in.readString();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInteger(id); …Run Code Online (Sandbox Code Playgroud)