gpt4 book ai didi

java - 是否可以在 Groovy 中提供自己类型的 Enums 实例变量?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:42:12 26 4
gpt4 key购买 nike

我正在用 Groovy 制作一个文本冒险游戏作为一种练习,但我遇到了一个奇怪的错误。

现在,我有一个 enum玩家可以去的方向,目前包括北、南、东、西、上和下。

我有一个 Room包含 Map 的类其他相连的房间及其方向。当我添加 Room到另一个Room在某个Direction ,我还希望能够添加当前的 Room给另一个Room相反方向。

例如:如果我添加从 Room 1 到 Room 2 向北的连接,我希望能够同时添加从 Room 2 到 Room 1 向南的连接。

目前,我正在尝试使用 enum 来实现它名为 Direction带有附加的实例变量 opposite (类型 Direction )。这是不允许的吗?我没有收到编译器错误或任何其他错误,但我似乎无法让它工作。

这是完整的 enum声明:

public enum Direction {
North(South), South(North), East(West), West(East), Up(Down), Down(Up)
private Direction opposite
Direction(Direction d){
opposite = d
}
public opposite(){
return opposite
}
}

这是我调用它的方法:

public void addConnection(Direction d, Spot spot){
connections[d] = spot
spot.connections[d.opposite()] = this
}

哪里connectionspublic Map<Direction, Spot> .

在这种情况下,一个条目被添加到 connections看起来像这样:

null:Spot@some_hexadecimal_representation

任何帮助都会很棒。谢谢!

最佳答案

Groovy 似乎规避了 Java 中的 a compilation error :

Main.java:2: illegal forward reference
North(South), South(North), East(West), West(East), Up(Down), Down(Up);
^
Main.java:2: illegal forward reference
North(South), South(North), East(West), West(East), Up(Down), Down(Up);
^
Main.java:2: illegal forward reference
North(South), South(North), East(West), West(East), Up(Down), Down(Up);
^
3 errors

groovy 编译器不会对此提示,而是将需要前向声明的枚举值初始化为 null:

public enum Direction {
North(South), South(North), East(West), West(East), Up(Down), Down(Up)
Direction(Direction d){
println "opposite of $this is $d"
}
}

Direction.South // Force enum instantiation in GroovyConsole.

Outputs :

opposite of North is null
opposite of South is North
opposite of East is null
opposite of West is East
opposite of Up is null
opposite of Down is Up

一个似乎在 Java 中工作得很好的解决方案是 adding a static block on the Direction class初始化 opposite 值。转换为 Groovy,将是:

enum Direction {
North, South, East, West, Up, Down
private Direction opposite
Direction getOpposite() { opposite }

static {
def opposites = { d1, d2 -> d1.opposite = d2; d2.opposite = d1 }
opposites(North, South)
opposites(East, West)
opposites(Up, Down)
}
}

Direction.values().each {
println "opposite of $it is $it.opposite"
}

现在prints the correct values :

opposite of North is South
opposite of South is North
opposite of East is West
opposite of West is East
opposite of Up is Down
opposite of Down is Up

更新

Another ,也许更直接,解决方案可以使用枚举上的方向索引来找到对立面:

public enum Direction {
North(1), South(0), East(3), West(2), Up(5), Down(4)
private oppositeIndex
Direction getOpposite() {
values()[oppositeIndex]
}
Direction(oppositeIndex) {
this.oppositeIndex = oppositeIndex
}
}

但我发现第一个更清晰,因为它不需要索引的那些神奇数字呵呵。

更新2

现在,我可能有点喜欢这里的高尔夫球场,但你可以去相反的方向 without the need of an extra field ,仅使用枚举值的 ordinal()(它们的索引):

enum Direction {
North, South, East, West, Up, Down
Direction getOpposite() {
values()[ordinal() + ordinal() % 2 * -2 + 1]
}
}

它并没有看起来那么可怕!偶数方向(北、东、上)返回 ordinal() + 1 的相反方向,而奇数方向(其他方向)返回 ordinal() - 1。当然它在很大程度上依赖于枚举中元素的顺序,但是,你不喜欢这种简洁吗? =D

关于java - 是否可以在 Groovy 中提供自己类型的 Enums 实例变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11302278/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com