【发布时间】:2014-10-30 10:37:10
【问题描述】:
问题描述
我有实现Parcelable 的Category 类,还有一些从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);
dest.writeString(title);
}
public static final Parcelable.Creator<Category > CREATOR = new Parcelable.Creator<Category >() {
public Category createFromParcel(Parcel in) {
return new Category (in); <=== !!! THIS IS NOT ALLOWED AS CLASS IS ABSTRACT !!!
}
public Category [] newArray(int size) {
return new Category[size];
}
};
abstract void generateCodes();
abstract String getImageIcon();
};
public final class Taxi extends Category {
public Taxi() {
title = "taxi";
id = 1547845;
}
};
public final class Hotel extends Category {
public Hotel() {
title = "hotel";
id = 1397866;
}
};
【问题讨论】:
标签: java android abstract-class parcelable