简体   繁体   中英

Java Initiate Subclass Instances with Superclass Instance Data

I have a class FavoriteInfo that stores user favorites. It has a bunch of private fields and public gets:

public class FavoriteInfo implements Serializable {         
    public static String KEY ="FavoriteInfo";

    private String favtype;
    private String rdbkey;

    private String VideoURL;
    private String ImageURL;
    private Float Temperature;
    /* more such fields */

    public String getFavtype() {
        return favtype;
    }

    public void setFavtype(String favtype) {
        this.favtype = favtype;
    }

    public String getRdbkey() {
        return rdbkey;
    }

    public void setRdbkey(String rdbkey) {
        this.rdbkey = rdbkey;
    }

    public String getImageURL() {
        return ImageURL;
    }

    public void setImageURL(String imageURL) {
        this.ImageURL = imageURL;
    }

Then I have:

public abstract class MomentData extends FavoriteInfo {
    public static final int DATA_TYPE_IMAGE = 0;
    public static final int DATA_TYPE_VIDEO = 1;

    public abstract int getDataType();
    public abstract View getChildView(Context context,View convertView, ViewGroup parent);
}

And two public classes MomentImageData and MomentVideoData that extend MomentData.

My data comes from a HTML request that directly populates the fields in FavoriteInfo and returns a List of FavoriteInfo instances. This is the data source that I want to keep untouched and tap into. I don't have any additional fields except for DATA_TYPE_IMAGE and DATA_TYPE_VIDEO in the subclasses.

My question is how I can use this list to build a list of MomentImageData and a list of MomentVideoData ?

This is what I want to achieve:

List<MomentData> mData;
List<MomentImageData> imgData; // from one source 
List<MomentVideoData> videoData; // from another

mdata.addAll(imgData);
mdata.addAll(videoData);

This may look weird, but it's actually an Android app and I have a custom adapter that takes MomentData mData as the data source, and then it inflates different views based on the type (image or video) dynamically.

The most directly way to create a sub-class instance from a super-class instance is by copying the fields from the super-class instance into the sub-class instance

public MomentImageData(FavoriteInfo info) {
    setRdbkey(info.getRdbkey());
    // ... and all the others
}

An alternative is to use reflection or a deep-copy library to save you from writing all this setters.

Java won't do this four you, because a MomentImageData is a FavoriteInfo but not viceversa.

Another way is to use composition and not inheritance:

public class MomentImageData {
     FavoriteInfo info;

     public MomentImageData(FavoriteInfo info) {
          this.info = info;
     }
}

So you can easily create an instance like

List<FavoriteInfo> list = new ArrayList();
// fill the list
MomentImageData mid = new MomentImageData(list.get(1));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM