比较枚举项-Java
假设我有几台机器:比较枚举项-Java,java,inheritance,enums,Java,Inheritance,Enums,假设我有几台机器: //当然是测试代码。 公开课开始{ 公共静态void main(字符串参数[]){ System.out.println(Machine.COFFEE_GRINDER.getCatalogId()); System.out.println(Machine.COFFEE_Machine.isOfType(Machine.BASIC_Machine)); //上述情况应该是正确的。 } 专用枚举机{ 基本机器(-1), 饮料(-1), 咖啡(-1), 咖啡研磨机(5), 咖啡机(
//当然是测试代码。
公开课开始{
公共静态void main(字符串参数[]){
System.out.println(Machine.COFFEE_GRINDER.getCatalogId());
System.out.println(Machine.COFFEE_Machine.isOfType(Machine.BASIC_Machine));
//上述情况应该是正确的。
}
专用枚举机{
基本机器(-1),
饮料(-1),
咖啡(-1),
咖啡研磨机(5),
咖啡机(6),
花园(-1),
割草机(28);
私有最终int catalogId;
public int getCatalogId(){
返回catalogId;
}
公共布尔类型(机器到){
将此==返回到;
}
机器(int目录ID){
this.catalogId=catalogId;
}
}
}
在上面的示例中,有些机器与它们在目录中显示的一样,并且具有与之关联的id号。还有节和节的节。所以饮料机仍然是基本的机器。咖啡机仍然是饮料机
程序中的一些功能在执行其功能之前,必须检查机器是否实际上是一台beverage机器。目录中的咖啡研磨机和咖啡机都将被检查出来,功能也将完成
我所寻找的行为类似于instanceof或抽象类的继承。无论如何,咖啡机实际上是一种基本的咖啡机,我想检查一下
因此:
一种可能的解决方案是使用回调和保存的超类型来模拟某些继承。 我很好奇这是不是最好的办法 实现了如下所示:
//Test code of course.
public class Start{
public static void main(String args[]){
System.out.println(Machine.COFFEE_GRINDER.getCatalogId());
System.out.println(Machine.COFFEE_MACHINE.isOfType(Machine.BASIC_MACHINE)); //Should be true.
}
private enum Machine {
BASIC_MACHINE (-1),
BEVERAGE (-1, BASIC_MACHINE),
COFFEE(-1, BEVERAGE),
COFFEE_GRINDER (5, COFFEE),
COFFEE_MACHINE (6, COFFEE),
GARDEN (-1, BASIC_MACHINE),
LAWN_MOWER (28, GARDEN);
private int catalogId;
private Machine superMachine;
public int getCatalogId(){
return catalogId;
}
public Machine getSuperMachine(){
return superMachine;
}
//With callback to superMachine (if present)
public boolean isOfType(Machine to){
return this == to || (getSuperMachine() != null && getSuperMachine().isOfType(to));
}
Machine (int catalogId) {
this.catalogId = catalogId;
}
Machine (int catalogId, Machine superMachine) {
this(catalogId);
this.superMachine = superMachine;
}
}
}
一种可能的解决方案是使用回调和保存的超类型来模拟某些继承。 我很好奇这是不是最好的办法 实现了如下所示:
//Test code of course.
public class Start{
public static void main(String args[]){
System.out.println(Machine.COFFEE_GRINDER.getCatalogId());
System.out.println(Machine.COFFEE_MACHINE.isOfType(Machine.BASIC_MACHINE)); //Should be true.
}
private enum Machine {
BASIC_MACHINE (-1),
BEVERAGE (-1, BASIC_MACHINE),
COFFEE(-1, BEVERAGE),
COFFEE_GRINDER (5, COFFEE),
COFFEE_MACHINE (6, COFFEE),
GARDEN (-1, BASIC_MACHINE),
LAWN_MOWER (28, GARDEN);
private int catalogId;
private Machine superMachine;
public int getCatalogId(){
return catalogId;
}
public Machine getSuperMachine(){
return superMachine;
}
//With callback to superMachine (if present)
public boolean isOfType(Machine to){
return this == to || (getSuperMachine() != null && getSuperMachine().isOfType(to));
}
Machine (int catalogId) {
this.catalogId = catalogId;
}
Machine (int catalogId, Machine superMachine) {
this(catalogId);
this.superMachine = superMachine;
}
}
}
如果我正确理解了您的目标,那么您需要表示
enum
常量之间的关系网络。考虑继承不一定与你想要表达的关系一致。继承为类型之间的关系建模;您需要一个连接到实例之间的关系。可能使用与“超级/继承”不同的术语,例如“所有者拥有”。您可以在每个常量中嵌入一个指向所有者
实例的指针,并在静态初始值设定项和构造函数中创建一个非循环有向图结构
public enum NetworkedMachine
{
BASIC_MACHINE(-1, null),
BEVERAGE(-1, BASIC_MACHINE),
COFFEE(-1, BASIC_MACHINE),
COFFEE_GRINDER(5, COFFEE),
COFFEE_MACHINE(6, COFFEE),
GARDEN(-1, BASIC_MACHINE),
LAWN_MOWER(28, GARDEN),
;
static final Map<NetworkedMachine, Set<NetworkedMachine>> owners;
static {
Map<NetworkedMachine, Set<NetworkedMachine>> ownership = new HashMap<>();
for (NetworkedMachine machine : values()) {
ownership.put(machine, new HashSet<>());
}
for (NetworkedMachine machine : values()) {
if (machine.owner != null) {
ownership.get(machine.owner).add(machine);
}
}
for (NetworkedMachine machine : values()) {
Set<NetworkedMachine> owns = ownership.get(machine);
ownership.put(machine, Collections.unmodifiableSet(owned));
}
owners = Collections.unmodifiableMap(ownership);
}
private final int catalogId;
private final NetworkedMachine owner;
NetworkedMachine(int catalogId, NetworkedMachine machine) {
this.catalogId = catalogId;
this.owner = machine;
}
public int getCatalogId() {
return catalogId;
}
public NetworkedMachine getOwner() {
return owner;
}
public Set<NetworkedMachine> getOwns() {
return owners.get(this);
}
public boolean isOwned() {
return owner != null;
}
}
如果我正确理解了您的目标,那么您需要表示enum
常量之间的关系网络。考虑继承不一定与你想要表达的关系一致。继承为类型之间的关系建模;您需要一个连接到实例之间的关系。可能使用与“超级/继承”不同的术语,例如“所有者拥有”。您可以在每个常量中嵌入一个指向所有者
实例的指针,并在静态初始值设定项和构造函数中创建一个非循环有向图结构
public enum NetworkedMachine
{
BASIC_MACHINE(-1, null),
BEVERAGE(-1, BASIC_MACHINE),
COFFEE(-1, BASIC_MACHINE),
COFFEE_GRINDER(5, COFFEE),
COFFEE_MACHINE(6, COFFEE),
GARDEN(-1, BASIC_MACHINE),
LAWN_MOWER(28, GARDEN),
;
static final Map<NetworkedMachine, Set<NetworkedMachine>> owners;
static {
Map<NetworkedMachine, Set<NetworkedMachine>> ownership = new HashMap<>();
for (NetworkedMachine machine : values()) {
ownership.put(machine, new HashSet<>());
}
for (NetworkedMachine machine : values()) {
if (machine.owner != null) {
ownership.get(machine.owner).add(machine);
}
}
for (NetworkedMachine machine : values()) {
Set<NetworkedMachine> owns = ownership.get(machine);
ownership.put(machine, Collections.unmodifiableSet(owned));
}
owners = Collections.unmodifiableMap(ownership);
}
private final int catalogId;
private final NetworkedMachine owner;
NetworkedMachine(int catalogId, NetworkedMachine machine) {
this.catalogId = catalogId;
this.owner = machine;
}
public int getCatalogId() {
return catalogId;
}
public NetworkedMachine getOwner() {
return owner;
}
public Set<NetworkedMachine> getOwns() {
return owners.get(this);
}
public boolean isOwned() {
return owner != null;
}
}
您只需要从最专门的enum
值开始遍历继承路径,并检查每个步骤。听起来很琐碎。自动售货机
未定义。您的意思是饮料
?您不能分配Machine.COFFEE\u Machine=Machine.COFFEE等,因为枚举常量是常量。“实现某事?”如果你能描述一下你想要实现的“某事”,那会有所帮助。标题中提到了继承,但除了匿名主体类之外,您不能对枚举进行子类化,也不能说明继承与任何事情有什么关系。您不需要“检查类型”就知道每个常量(对象)的类型为机器。如图所示,您的代码将无法编译。请澄清您想要什么,并提供可编译的代码。@LewBloch我不认为OP试图在那里赋值。我把它理解为OP表示继承的符号,即,COFFEE\u MACHINE
“extends”COFFEE
,它在抽象意义上扩展了BEVERAGE
,扩展了BASIC\u MACHINE
,而不是Java类继承意义上的。这个问题写得很糟糕,我和你们一样想,直到我重新阅读这个问题。在我看来,OP似乎需要与instanceof
等效的实例来比较机器
枚举,例如咖啡机
是饮料
的实例,但割草机
不是咖啡
的实例。你们说得对。我模糊不清。我希望这能让事情变得更清楚一点。我也已经有了一个解决方案,但我不确定它是否有用。您只需要从最专门的enum
值开始遍历继承路径,然后检查每一步。听起来很琐碎。自动售货机
未定义。您的意思是饮料
?您不能分配Machine.COFFEE\u Machine=Machine.COFFEE等,因为枚举常量是常量。“实现某事?”如果你能描述一下你想要实现的“某事”,那会有所帮助。标题中提到了继承,但除了匿名主体类之外,您不能对枚举进行子类化,也不能说明继承与任何事情有什么关系。您不需要“检查类型”就知道每个常量(对象)的类型为机器。如图所示,您的代码将无法编译。请澄清您想要什么,并提供可编译的代码。@LewBloch我不认为OP试图在那里赋值。我把它理解为OP表示继承的符号,即,COFFEE\u MACHINE
“extends”COFFEE
,它在抽象意义上扩展了BEVERAGE
,扩展了BASIC\u MACHINE
,而不是Java类继承意义上的。这个问题写得很糟糕,我和你们一样想,直到我重新阅读这个问题。在我看来,OP似乎需要与instanceof
等效的实例来比较机器
枚举,例如咖啡机