为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

拉莫斯之舞 2022-07-20 16:13:40
我有 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();}@Overridepublic 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);}
查看完整描述

1 回答

?
白猪掌柜的

TA贡献1893条经验 获得超10个赞

我发现了一些不同但有效的解决方案!


我创建一个界面


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);


查看完整回答
反对 回复 2022-07-20
  • 1 回答
  • 0 关注
  • 94 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号