猿问

如何使用接口正确实现多态性?

我有 2 个模型类(数据,标题),它们包含相同的字段:字符串数据 ID。我想通过接口实现获得这两个 ID。


我通过 Bundle 将 Title 模型传递给另一个 Activity,在同一个 Activity 中通过 Bundle 传递 Data 模型(只是创建 Activity 的新实例并重置信息)。


我希望我的两个模型类都使用方法 String getSharedId(); 实现 SharedID 接口;


我怎样才能从不同的模型中获得不同的 id?我只需要输入一个参数,它应该是我的 ViewModelFactory 构造函数中的字符串。


public class Data implements SharedId,Parcelable {


private String text;

private String textHeader;

private int viewType;

private String mainId;

private String dataID;


public Data() { }


public String getDataID() {

    return dataID;

}


public void setDataID(String dataID) {

    this.dataID = dataID;

}


public String getText() {return (String) trimTrailingWhitespace(text); }


public void setText(String text) {

    this.text = (String) trimTrailingWhitespace(text);

}


public String getTextHeader() {

    return (String) trimTrailingWhitespace(textHeader);

}


public void setTextHeader(String textHeader) {

    this.textHeader = textHeader;

}


public int getViewType() {

    return viewType;

}


public void setViewType(int viewType) {

    this.viewType = viewType;

}


public String getMainId() {

    return mainId;

}


public void setMainId(String mainId) {

    this.mainId = mainId;

}


protected Data(Parcel in) {

    text = in.readString();

    textHeader = in.readString();

    viewType = in.readInt();

    mainId = in.readString();

    dataID = in.readString();

}


@Override

public String toString() {

    return "Data{" +

            "order=" +

            ", text='" + text + '\'' +

            ", textHeader='" + textHeader + '\'' +

            ", viewType=" + viewType +

            '}';

}


@SuppressWarnings("StatementWithEmptyBody")

public static CharSequence trimTrailingWhitespace(CharSequence source) {

    if (source == null) {

        return "";

    }

    int i = source.length();

    // loop back to the first non-whitespace character

    while (--i >= 0 && Character.isWhitespace(source.charAt(i))) {

    }

    return source.subSequence(0, i + 1);

}


拉莫斯之舞
浏览 110回答 1
1回答

白猪掌柜的

我发现了一些不同但有效的解决方案!我创建一个界面public interface SharedId { String getSharedDataId(); String getHeader();}我的两个模型类 Data + Title 都实现了接口和方法。在 DetailActivity 我创建了 2 个字符串。私有字符串 mainId;私有字符串 detailId;然后用我的模型类和 bundle 传递 id`SharedId mainId = new Title();    SharedId detailId = new Data();    Bundle bundle = getIntent().getExtras();    if (bundle != null) {        mainId = bundle.containsKey("ID") ? bundle.getParcelable("ID") : null;        detailId = bundle.containsKey("idDetail") ?      bundle.getParcelable("idDetail") : null;    }    if (mainId != null) {        this.detailId = mainId.getSharedDataId();        tvToolbarTitle.setText(mainId.getHeader());    }    if (detailId != null) {        this.mainId = detailId.getSharedDataId();        tvToolbarTitle.setText(detailId.getHeader());    }并传入我的 ViewmodelFactory DetailViewModelFactory detailViewModelFactory =            new DetailViewModelFactory(this.detailId != null ?            this.detailId : this.mainId);
随时随地看视频慕课网APP

相关分类

Java
我要回答