我有一个名为Direction的枚举类,它有对象S, N, W, E, and none
(南,挪威,东,西,没有)。我有一个方法public boolean isOpposite(Direction other)
,它应该报告给定方向是否与另一方向相反。没有一个与其他任何东西相反,但N / S是对立的,E / W是对立的。那么,如果方向与传递的对象相反,我如何检查布尔值?
public enum Direction {
N, E, S, W, none;
public Direction cycle()
{
if (this.equals(Direction.N))
return Direction.E;
else if(this.equals(Direction.E))
return Direction.S;
else if(this.equals(Direction.S))
return Direction.W;
else if(this.equals(Direction.W))
return Direction.N;
else if(this.equals(Direction.none))
return none;
return null;
}
public Direction getOpposite()
{
if(this.equals(Direction.N))
return Direction.S;
else if(this.equals(Direction.S))
return Direction.N;
else if(this.equals(Direction.E))
return Direction.W;
else if(this.equals(Direction.W))
return Direction.E;
else if(this.equals(Direction.none))
return Direction.none;
return null;
}
public boolean isOpposite(Direction other)
{
//???
}
}
答案 0 :(得分:1)
return getOpposite() == other;