[英]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
,然后讓GUIItemTypeCommand
和GUIItemTypeSend
實現它。
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.