抽象クラス\インタフェースを使用して

user11731595:

私はにより確認することが何リットルオブジェクトを作成する必要がある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;
    }
  }
}

Whay私はそれを作成するために使用する必要が?GUIItemTypeCommandクラス必見GUIItemTypeクラスを拡張しますか?GUIItemTypeクラスは、抽象的でなければなりませんか?

アービンド・クマールのAvinash:

あなたは、作成することができますGUIItemTypeようinterface、次に作るGUIItemTypeCommandGUIItemTypeSendそれを実装します。

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

class GUIItemTypeCommand implements GUIItemType {

}

class GUIItemTypeSend implements GUIItemType {

}

おすすめ

転載: http://10.200.1.11:23101/article/api/json?id=394050&siteId=1