【问题标题】:Android abstract parcelable classAndroid 抽象可打包类
【发布时间】:2014-10-30 10:37:10
【问题描述】:

问题描述

我有实现ParcelableCategory 类,还有一些从Category 类扩展而来的类。我的基类有 2 个protected 成员titleid,它们主要是从继承的类中设置的。因此,为了不在继承的类中到处实现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


    【解决方案1】:

    可以在抽象类中使用构造函数,您可以在那里进行打包 - 只需确保在子类中调用相应的 super(...) 方法即可。 签出this

    编辑: 我认为 Category 不需要实现 CREATOR 因为你不能实例化它......?类似的建议是herehere

    public abstract class Category implements Parcelable {
        private static Map<Integer, Category> categoryMap = new TreeMap<Integer, Category>();
    
        protected String title;
        protected Integer  id;
    
        static {
            categoryMap.put(0, new Taxi());
            categoryMap.put(1, new Hotel());
        }
    
        protected Category(){}
    
        protected 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);
        }
    
        abstract void generateCodes();
        abstract String getImageIcon();
    
    };
    
    public final class Taxi extends Category {
        public Taxi() {
            title = "taxi";
            id = 1547845;
        }
    
        protected Taxi(Parcel in) {
            super(in);
        }
    
        public static final Parcelable.Creator<Taxi> CREATOR = new Parcelable.Creator<Taxi>() {
            public Category createFromParcel(Parcel in) {
                return new Taxi (in);
            }
    
            public Category [] newArray(int size) {
                return new Taxi[size];
            }
        };
    };
    
    public final class Hotel extends Category {
        public Hotel() {
            title = "hotel";
            id = 1397866;
        }
    
        protected Hotel(Parcel in) {
            super(in);
        }
    
        public static final Parcelable.Creator<Hotel> CREATOR = new Parcelable.Creator<Hotel>() {
            public Category createFromParcel(Parcel in) {
                return new Hotel (in);
            }
    
            public Category [] newArray(int size) {
                return new Hotel[size];
            }
        };
    };
    

    【讨论】:

    • public Category createFromParcel(Parcel in) { return new Category(in);} 但我不想在子类中而是在基类本身中调用它。
    • 用代码示例更新了答案 - 但它确实两次指定了 CREATOR。如果你想把创建者放在基类中,那么它本身必须知道它的孩子,这在技术上是可行的,但不是一个很好的模式。
    • 对不起,我的声誉很低,我可以给你+1,给你+1,谢谢你的回答,这对我很有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多