Java 如何使用接口正确地实现多态性?
我有两个模型类(数据、标题),它们包含相同的字段: 字符串数据标识。我想通过接口实现获得这两个ID 我将标题模型通过Bundle传递给另一个活动,将数据模型通过该活动中的Bundle传递(只是创建活动的新实例并重置信息) 我希望我的两个模型类都使用方法字符串getSharedId()实现SharedID接口 如何从不同的型号获得不同的ID?我只需要放置一个参数,它应该是ViewModelFactory构造函数中的字符串Java 如何使用接口正确地实现多态性?,java,android,interface,model,polymorphism,Java,Android,Interface,Model,Polymorphism,我有两个模型类(数据、标题),它们包含相同的字段: 字符串数据标识。我想通过接口实现获得这两个ID 我将标题模型通过Bundle传递给另一个活动,将数据模型通过该活动中的Bundle传递(只是创建活动的新实例并重置信息) 我希望我的两个模型类都使用方法字符串getSharedId()实现SharedID接口 如何从不同的型号获得不同的ID?我只需要放置一个参数,它应该是ViewModelFactory构造函数中的字符串 public class Data implements SharedId,
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);
}
public static final Creator<Data> CREATOR = new Creator<Data>() {
@Override
public Data createFromParcel(Parcel in) {
return new Data(in);
}
@Override
public Data[] newArray(int size) {
return new Data[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(text);
dest.writeString(textHeader);
dest.writeInt(viewType);
dest.writeString(mainId);
dest.writeString(dataID);
}
@Override
public String getSharedDataId() {
return getDataID();
}
公共类数据实现SharedId,可包裹{
私有字符串文本;
私有字符串文本头;
私有int视图类型;
私有字符串mainId;
私有字符串数据标识;
公共数据(){}
公共字符串getDataID(){
返回数据标识;
}
public void setDataID(字符串dataID){
this.dataID=dataID;
}
公共字符串getText(){return(String)trimTrailingWhitespace(text);}
公共void setText(字符串文本){
this.text=(字符串)trimTrailingWhitespace(文本);
}
公共字符串getTextHeader(){
返回(字符串)trimTrailingWhitespace(textHeader);
}
公共无效setTextHeader(字符串textHeader){
this.textHeader=textHeader;
}
public int getViewType(){
返回视图类型;
}
公共void setViewType(int viewType){
this.viewType=viewType;
}
公共字符串getMainId(){
返回mainId;
}
public void setMainId(字符串mainId){
this.mainId=mainId;
}
受保护数据(包裹中){
text=in.readString();
textHeader=in.readString();
viewType=in.readInt();
mainId=in.readString();
dataID=in.readString();
}
@凌驾
公共字符串toString(){
返回“数据{”+
“订单=”+
“,text=”+text+“\”+
“,textHeader='”+textHeader+'\''+
,viewType=“+viewType+
'}';
}
@SuppressWarnings(“StatementWithEmptyBody”)
公共静态CharSequence trimTrailingWhitespace(CharSequence源){
if(source==null){
返回“”;
}
int i=source.length();
//循环回第一个非空白字符
而(--i>=0&&Character.isWhitespace(source.charAt(i))){
}
返回源。子序列(0,i+1);
}
公共静态最终创建者=新创建者(){
@凌驾
公共数据createFromParcel(地块中){
返回新数据(在中);
}
@凌驾
公共数据[]新数组(整数大小){
返回新数据[大小];
}
};
@凌驾
公共int描述内容(){
返回0;
}
@凌驾
公共无效写入包裹(包裹目的地,内部标志){
目的书面限制(文本);
目的写入字符串(textHeader);
目标写入(视图类型);
目的写入目录(mainId);
目的写入限制(数据标识);
}
@凌驾
公共字符串getSharedDataId(){
返回getDataID();
}
}
公共类标题实现SharedId,可包裹{
私有字符串数据标识;
私有字符串标题;
公共标题(){}
受保护的所有权(包裹中){
dataID=in.readString();
title=in.readString();
}
公共字符串getDataID(){
返回数据标识;
}
public void setDataID(字符串dataID){
this.dataID=dataID;
}
公共字符串getTitle(){
返回标题;
}
公共无效集合标题(字符串标题){
this.title=标题;
}
公共静态最终创建者=新创建者(){
@凌驾
公共标题createFromParcel(中的地块){
返回新标题(在中);
}
@凌驾
公共标题[]新数组(整数大小){
返回新标题[大小];
}
};
@凌驾
公共int描述内容(){
返回0;
}
@凌驾
公共无效写入包裹(包裹目的地,内部标志){
目的写入限制(数据标识);
目的书面记录(标题);
}
@非空
@凌驾
公共字符串toString(){
返回“Title{”+
“dataID=”+dataID+“\”+
,标题顺序=“+
“,title='”+title+'\''+
'}';
}
@凌驾
公共字符串getSharedDataId(){
返回getDataID();
}
}
还有我的DetailActivity代码,我已经成功地完成了传递id的任务,但是我需要通过接口来完成这项任务:(所以请帮助我,朋友们,非常感谢
public class DetailActivity extends AppCompatActivity implements
DetailAdapter.OnDialogClickListener,
DetailAdapter.OnDetailClickListener {
private static String id;
private String parentId;
private Data data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
TextView tvToolbarTitle = findViewById(R.id.title_toolbar_detail);
tvToolbarTitle.setSelected(true);
findViewById(R.id.btn_back).setOnClickListener(v -> finish());
ArrayList<SharedId> sharedIds = new ArrayList<>();
sharedIds.add(new Title());
sharedIds.add(new Data());
for (SharedId sharedId : sharedIds){
System.out.println(sharedId.getSharedDataId());
}
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
Title model = bundle.containsKey("ID") ? bundle.getParcelable("ID") : null;
Data childModel = bundle.containsKey("idDetail") ? bundle.getParcelable("idDetail") : null;
}
if (bundle != null) {
Title model = bundle.containsKey("ID") ? bundle.getParcelable("ID") : null;
Data childModel = bundle.containsKey("idDetail") ? bundle.getParcelable("idDetail") : null;
String parentId = bundle.getString("mainScreenId");
if (parentId != null) {
this.parentId = parentId;
}
if (model != null) {
this.id = model.getDataID();
tvToolbarTitle.setText(model.getTitle());
}
if (childModel != null) {
this.id = childModel.getDataID();
tvToolbarTitle.setText(childModel.getTextHeader());
}
}
RecyclerView recyclerView = findViewById(R.id.rv_detail);
DetailAdapter adapter = new DetailAdapter(this, this);
recyclerView.setAdapter(adapter);
// TODO: 3/1/19 change it to single ID // DetailViewModelFactory(); // id != null ? id : parentId
DetailViewModelFactory detailViewModelFactory = new DetailViewModelFactory(id != null ? id : parentId);
DetailActivityViewModel viewModel = ViewModelProviders.of(this, detailViewModelFactory).get(DetailActivityViewModel.class);
FirebaseListLiveData<Data> liveData = viewModel.getLiveDataQuery();
liveData.observe(this, adapter::setNewData);
}
@Override
public void onDialogClicked(@NonNull String text) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(HtmlCompat.fromHtml(text, 0, null, new HandlerHtml()));
builder.setPositiveButton("Ok", null);
builder.show();
}
@Override
public void onDetailClicked(Data data) {
Intent intent = new Intent();
DetailActivity.open(DetailActivity.this);
intent.putExtra("idDetail", data);
intent.putExtra("mainScreenId", id);
startActivity(intent);
}
public static void open(@NonNull Context context) {
context.startActivity(new Intent(context, InfoActivity.class));
}
}
public类DetailActivity扩展了AppCompatActivity实现
DetailAdapter.OnDialogClickListener,
DetailAdapter.OnDetailClickListener{
私有静态字符串id;
私有字符串parentId;
私人数据;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_详细信息);
TextView tvToolbarTitle=findViewById(R.id.title\u toolbar\u detail);
tVToolbar title.setSelected(true);
findviewbyd(R.id.btn_back).setOnClickListener(v->finish());
ArrayList sharedIds=新的ArrayList();
添加(新标题());
添加(新数据());
for(SharedId SharedId:SharedId){
System.out.println(sharedId.getSharedDataId());
}
Bundle Bundle=getIntent().getExtras();
if(bundle!=null){
标题模型=bundle.containsKey(“ID”)?bundle.getParcelable(“ID”):null;
Data childModel=bundle.containsKey(“idDetail”)?bundle.getParcelable(“idDetail”):null;
}
if(bundle!=null){
标题模型=bundle.containsKey(“ID”)?bundle.getParcelable(“ID”):null;
Data childModel=bundle.containsKey(“idDetail”)?bundle.getParcelable(“idDetail”):null;
String parentId=bundle.getString(“mainScreenId”);
if(parentId!=null){
this.parentId=parentId;
}
如果(型号!=null){
this.id=model.getDataID();
tvToolbarTitle.setText(model.getTitle());
}
if(childModel!=null){
this.id=childModel.getDataID();
tvToolbarTitle.setText(childModel.getTextHeader());
}
}
RecyclerView RecyclerView=findViewById(R.id.rv_详图);
细节适配器适配
public class DetailActivity extends AppCompatActivity implements
DetailAdapter.OnDialogClickListener,
DetailAdapter.OnDetailClickListener {
private static String id;
private String parentId;
private Data data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
TextView tvToolbarTitle = findViewById(R.id.title_toolbar_detail);
tvToolbarTitle.setSelected(true);
findViewById(R.id.btn_back).setOnClickListener(v -> finish());
ArrayList<SharedId> sharedIds = new ArrayList<>();
sharedIds.add(new Title());
sharedIds.add(new Data());
for (SharedId sharedId : sharedIds){
System.out.println(sharedId.getSharedDataId());
}
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
Title model = bundle.containsKey("ID") ? bundle.getParcelable("ID") : null;
Data childModel = bundle.containsKey("idDetail") ? bundle.getParcelable("idDetail") : null;
}
if (bundle != null) {
Title model = bundle.containsKey("ID") ? bundle.getParcelable("ID") : null;
Data childModel = bundle.containsKey("idDetail") ? bundle.getParcelable("idDetail") : null;
String parentId = bundle.getString("mainScreenId");
if (parentId != null) {
this.parentId = parentId;
}
if (model != null) {
this.id = model.getDataID();
tvToolbarTitle.setText(model.getTitle());
}
if (childModel != null) {
this.id = childModel.getDataID();
tvToolbarTitle.setText(childModel.getTextHeader());
}
}
RecyclerView recyclerView = findViewById(R.id.rv_detail);
DetailAdapter adapter = new DetailAdapter(this, this);
recyclerView.setAdapter(adapter);
// TODO: 3/1/19 change it to single ID // DetailViewModelFactory(); // id != null ? id : parentId
DetailViewModelFactory detailViewModelFactory = new DetailViewModelFactory(id != null ? id : parentId);
DetailActivityViewModel viewModel = ViewModelProviders.of(this, detailViewModelFactory).get(DetailActivityViewModel.class);
FirebaseListLiveData<Data> liveData = viewModel.getLiveDataQuery();
liveData.observe(this, adapter::setNewData);
}
@Override
public void onDialogClicked(@NonNull String text) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(HtmlCompat.fromHtml(text, 0, null, new HandlerHtml()));
builder.setPositiveButton("Ok", null);
builder.show();
}
@Override
public void onDetailClicked(Data data) {
Intent intent = new Intent();
DetailActivity.open(DetailActivity.this);
intent.putExtra("idDetail", data);
intent.putExtra("mainScreenId", id);
startActivity(intent);
}
public static void open(@NonNull Context context) {
context.startActivity(new Intent(context, InfoActivity.class));
}
}
public interface SharedId {
String getSharedDataId();
String getHeader();
}
`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());
}
DetailViewModelFactory detailViewModelFactory =
new DetailViewModelFactory(this.detailId != null ?
this.detailId : this.mainId);