gpt4 book ai didi

Java 如何在构造函数中将接口(interface)作为参数传递?

转载 作者:行者123 更新时间:2023-12-02 06:48:50 24 4
gpt4 key购买 nike

我正在创建一个已在 GWT Showcase 中使用的复合小部件。

new DialogBoxWidget(constants);

//这里我遇到了错误:构造函数 DialogBoxWidget(ShowcaseConstants) 未定义。

/**
* Constants used throughout the showcase.
*/
public interface ShowcaseConstants extends MenuConstants,
CwDialogBox.CwConstants{
/**
* The path to source code for examples, raw files, and style definitions.
*/
String DST_SOURCE = "gwtShowcaseSource/";
}

这是我的 DialogBoxWidget.java,它是 Composite

 @ShowcaseSource
public static interface CwConstants extends Constants {
String cwDialogBoxCaption();

String cwDialogBoxClose();

String cwDialogBoxDescription();

String cwDialogBoxDetails();

String cwDialogBoxItem();

String cwDialogBoxListBoxInfo();

String cwDialogBoxMakeTransparent();

String cwDialogBoxName();

String cwDialogBoxShowButton();
}

/**
* An instance of the constants.
*/
@ShowcaseData
private final CwConstants constants;

/**
* Constructor.
* @param constants2
*
* @param constants the constants
*/
public DialogBoxWidget(CwConstants constants) {

// Add a hyper link to each section in the Widgets category
ShowcaseConstants allConstants = GWT.create(ShowcaseConstants.class);
this.constants = constants;
// Create the dialog box
final DialogBox dialogBox = createDialogBox();
dialogBox.setGlassEnabled(true);
dialogBox.setAnimationEnabled(true);

// Create a button to show the dialog Box
Button openButton = new Button(
allConstants.cwDialogBoxShowButton(), new ClickHandler() {
public void onClick(ClickEvent sender) {
dialogBox.center();
dialogBox.show();
}
});

// Create a ListBox
HTML listDesc = new HTML(
"<br><br><br>" + allConstants.cwDialogBoxListBoxInfo());

ListBox list = new ListBox();
list.setVisibleItemCount(1);
for (int i = 10; i > 0; i--) {
list.addItem(allConstants.cwDialogBoxItem() + " " + i);
}

// Add the button and list to a panel
VerticalPanel vPanel = new VerticalPanel();
vPanel.setSpacing(8);
vPanel.add(openButton);
vPanel.add(listDesc);
vPanel.add(list);
initWidget(vPanel);
}

下面是onModuleLoad()的代码

ShowcaseConstants constants = GWT.create(ShowcaseConstants.class);
@ShowcaseSource
public void onModuleLoad() {

DialogBoxWidget dialogBox = new DialogBoxWidget(constants);//Here I am stucked with error : The constructor DialogBoxWidget(ShowcaseConstants) is undefined
//Add it to the RootPanel.
RootPanel.get().add(dialogBox);
}

最佳答案

将构造函数更改为

public DialogBoxWidget(ShowcaseConstants  constants) {

您构造器接受CwConstants

public DialogBoxWidget(CwConstants constants) {

并且您正在尝试传递 ShowcaseConstants

或创建

CwConstants constants = GWT.create(CwConstants.class);

并传递这个。

关于Java 如何在构造函数中将接口(interface)作为参数传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18287681/

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