Java如何在構造函數中將接口作為參數傳遞? (Java How do I pass interface as parameter in Constructor?)


問題描述

Java如何在構造函數中將接口作為參數傳遞? (Java How do I pass interface as parameter in Constructor?)

I am creating a Composite widgets that have been used in GWT Showcase.

new DialogBoxWidget(constants);

//Here I am stucked with error : The constructor DialogBoxWidget(ShowcaseConstants) is undefined.

/**
* 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/";
}

Here is my DialogBoxWidget.java that is 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);
}

Below is the code of 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);
}

參考解法

方法 1:

Change your constructor to 

public DialogBoxWidget(ShowcaseConstants  constants) {

You construtor accepting  CwConstants 

public DialogBoxWidget(CwConstants constants) {

and you are trying to pass ShowcaseConstants  

or create 

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

and pass this.

方法 2:

I'd bet you get your refactoring/copy‑paste wrong: ShowcaseConstants extends CwDialogBox.CwConstants instead of DialogBoxWidget.CwConstants.

(by MaNnSuresh AttaThomas Broyer)

參考文件

  1. Java How do I pass interface as parameter in Constructor? (CC BY‑SA 3.0/4.0)

#java #gwt






相關問題

電子郵件地址中帶有 + 字符的 Java 郵件 (Java mail with + character in email address)

如何快速原型化 Java 代碼? (How to quickly prototype Java code?)

如何使用 Maven 在目標(SVN-)服務器上創建 Javadoc? (How to create Javadoc on the target (SVN-) server using Maven?)

為什麼檢查二叉樹有效性的解決方案不起作用? (Why the solution for checking the validity of binary tree is not working?)

Selenium webdriver通過第一個數字找到texy (Selenium webdriver find texy by first digits)

setOnClickListener 沒有在圖像視圖上被調用 (setOnClickListener is not getting called on image view)

繪製多邊形:找不到錯誤 (Drawing Polygon : unable to find error)

半透明 JButton:對像出現在背景中 (Semi-Transparent JButton: Objects appear in Background)

比較同一數組的元素 (Compare elements of the same array)

Java 屏幕截圖小程序 (Java screen capture applet)

Minecraft 1.8.9 Forge Modding 的Java 開發工具包,需要什麼JDK/JRE,代碼是否正確? (Java Development Kit with Minecraft 1.8.9 Forge Modding, What JDK/JRE Is Needed, Is Code Correct?)

java while (resultset.next()) 不返回同一列中的所有數據 (java while (resultset.next()) does not return all data in the same column)







留言討論