创建枚举后,我想分配一个随机值并将其作为参数发送给函数,但我遇到了麻烦。
public enum TGate
{
A, B, C, D
}
public class Parking
{
TGate gate;
gate=TGate.C;
public Parking(TGate gate)
{
switch gate.....
}
}
我希望有人能帮助我。由于 你可以这样写:
TGate gate;
gate = TGate.values()[(int)(Math.random()*TGate.values().length)];
您可以这样尝试:
public class Example {
public static void main(String[] args) {
new Parking(TGate.values()[(int) (Math.random() * TGate.values().length)]);
}
}
enum TGate {
A, B, C, D
}
class Parking {
private final TGate gate;
public Parking(TGate gate) {
this.gate = gate;
}
}