【发布时间】:2017-01-19 08:24:55
【问题描述】:
我想创建一个类来定义一个字符串变量,该变量只能有几个预定义的值。
public class Constraint{
String const; // I want this constraint value to be fixed to either of {greaterthan,lesserthan,greaterthanequalto or lesserthanequalto}
public Constraint(String const){
this.const = const;
}
}
如果发送任何其他值,程序应该抛出错误。 我想在这里使用枚举之类的东西,但我想为字符串这样做。
【问题讨论】:
-
您不能,但您可以使用
enum代替。 Check this