簡體   English   中英

Java如何在構造函數中將接口作為參數傳遞?

[英]Java How do I pass interface as parameter in Constructor?

我正在創建一個已在GWT Showcase中使用的Composite小部件。

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

你是construtor接受CwConstants

public DialogBoxWidget(CwConstants constants) {

並且您正在嘗試傳遞ShowcaseConstants

或創造

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

通過這個。

我敢打賭你的重構/復制粘貼錯誤: ShowcaseConstants擴展了CwDialogBox.CwConstants而不是DialogBoxWidget.CwConstants

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM