簡體   English   中英

使用抽象類\\接口

[英]Using abstract classes \ interfaces

我需要創建一個可以通過instanceof關鍵字檢查的對象。

喜歡

public class Example {
  private int create(GUIItemType type){
    if(type instanceof GUIItemTypeCommand){
      return 0;
    } else if (type instanceof GUIItemTypeSend){
      return 1;
    } else {
      return 2;
    }
  }
}

我需要用什么來創建它? GUIItemTypeCommand 類必須擴展 GUIItemType 類嗎? GUIItemType 類必須是抽象的嗎?

您可以創建GUIItemType作為interface ,然后讓GUIItemTypeCommandGUIItemTypeSend實現它。

interface GUIItemType {
    // default methods, abstract methods, public static final variables
}

class GUIItemTypeCommand implements GUIItemType {

}

class GUIItemTypeSend implements GUIItemType {

}

暫無
暫無

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

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