我需要创建一个可以通过 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 {
}
关于java - 使用抽象类\接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60975359/