![](/img/trans.png)
[英]Java cast object to an interface that is not implemented but matches signature
[英]Java, passing object of implemented interface
下面的代碼段來自smartgwt的網站。 在調用接口ButtonClickHandler的addButtonClickHandler實現方法時,它作為匿名對象傳遞。在Java中,這個概念叫什么?
final Dialog dialog = new Dialog();
dialog.setMessage("Please choose whether to proceed");
dialog.setIcon("[SKIN]ask.png");
dialog.setButtons(new Button("OK"), new Button("Cancel"));
dialog.addButtonClickHandler(new ButtonClickHandler() {
public void onButtonClick(ButtonClickEvent event) {
dialog.hide();
}
});
dialog.draw();
ButtonClickHandler是一個接口,其中onButtonClick是唯一方法。
此概念稱為Anonymous Inner Classes
- 不指定名稱的內部類
匿名類使您可以使代碼更簡潔。 它們使您可以同時聲明和實例化一個類。 它們就像本地類,只是它們沒有名稱
這在編寫事件偵聽器 (如Swing,GWT等)中更常用。
匿名類通常在圖形用戶界面(GUI)應用程序中使用。
匿名班
您不必創建實現作為單獨文件中的參數所必需的接口的類,然后對其進行初始化並傳遞它。 您可以根據需要在同一行中執行此操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.