gpt4 book ai didi

java - 类如何调用具有由参数定义的名称的方法?

转载 作者:行者123 更新时间:2023-12-01 16:41:29 25 4
gpt4 key购买 nike

我是Java新手,所以我不太了解。我创建了一个类,当类收到积极输入时,它会激活一个方法。看起来像这样。


public class ExampleDialog extends AppCompatDialogFragment {

private static final String ARGUMENT_TITLE = "title";
private static final String ARGUMENT_POSITIVE = "positive";
private static final String ARGUMENT_MESSAGE = "message";
private static final String ARGUMENT_POSITIVE_TEXT = "positive_text";
private static final String ARGUMENT_NEGATIVE = "negative";
private static final String ARGUMENT_NEGATIVE_TEXT = "negative_text";
private static final String ARGUMENT_TAG_ID = "tag id";

private ExampleDialogListener listener;

private String title;
private String message;
private String positive;
private String positivetext;
private String negative;
private String negativetext;
private String tagid;

public static ExampleDialog newInstance(String title, String message, String positive,
String positivetext, String negative, String negativetext,) {
Bundle args = new Bundle();
// Store all arguments into bundle.
args.putString(ARGUMENT_TITLE, title); //save as name ARGUMENT_TITLE, value is the user input title, shove inside a bundle called args
args.putString(ARGUMENT_POSITIVE, positive);
args.putString(ARGUMENT_MESSAGE, message);
args.putString(ARGUMENT_POSITIVE_TEXT, positivetext);
args.putString(ARGUMENT_NEGATIVE, negative);
args.putString(ARGUMENT_NEGATIVE_TEXT, negativetext);
ExampleDialog fragment = new ExampleDialog();
fragment.setArguments(args);
return fragment;
}


@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {

title = getArguments().getString(ARGUMENT_TITLE);
positive = getArguments().getString(ARGUMENT_POSITIVE);
message = getArguments().getString(ARGUMENT_MESSAGE);
positivetext = getArguments().getString(ARGUMENT_POSITIVE_TEXT);
negative = getArguments().getString(ARGUMENT_NEGATIVE);
negativetext = getArguments().getString(ARGUMENT_NEGATIVE);

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(title)
.setMessage(message)
.setNegativeButton(negative, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast toast = Toast.makeText(getContext(), negativetext, Toast.LENGTH_SHORT);
toast.show();
}
})
.setPositiveButton(positive, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast toast = Toast.makeText(getContext(), positivetext, Toast.LENGTH_SHORT);
toast.show();
listener.onYesClicked();
}
});
return builder.create();
}

public interface ExampleDialogListener {
void onYesClicked();
}

@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);

try {
listener = (ExampleDialogListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()
+ "must implement ExampleDialogListener");
}
}

要使用此类,我必须调用执行以下操作

ExampleDialog overwritedialog = ExampleDialog.newInstance("Alert", "This will overwrite current save",
"Overwrite", "Override successful","Cancel",
"Cancelled");
overwritedialog.show(getSupportFragmentManager(), "example dialog");

然后实现一个方法onYesClicked,其中包含按下积极按钮时我想要发生的事情。

但是,我希望能够使用多个 ExampleDialog,但该类当前仅监听一个方法,即 onYesClicked。我希望能够将该类用作

ExampleDialog overwritedialog = ExampleDialog.newInstance("Alert", "This will overwrite current save",
"Overwrite", "Override successful","Cancel",
"Cancelled", "DialogA");
overwritedialog.show(getSupportFragmentManager(), "example dialog");

这样我就可以调用方法 DialogA 而不是 onYesClicked。我该怎么办?

最佳答案

简单的解决方案是仅使用 if-else 或 switch case 来使您的类根据传递的参数表现不同。

if(argument.equals("DialogA"))
doAstuff();
else if(argument.equas("DialogB"))
doBstuff();

更高级的解决方案是使用继承来构建默认对象,然后使用您需要的不同功能专门化子类。然后您可以使用拟合函数而不是字符串来传递想要的子类。

class DialogA extends Dialog

ExampleDialog(..., Dialog dialog){
dialog.execute()
//or whatever you want to name the function to do something with it. Has to be
// defined in superclass Dialog and overridden by DialogA
}

ExampleDialog.newInstance(..., new DialogA);

实际上,接口(interface)可能是比父类(super class)更好的选择。无论如何,要么使用简单的 if else 情况,要么使用继承/抽象。

关于java - 类如何调用具有由参数定义的名称的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61852259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com