如何限制只使用一个THUMNAIL?这意味着如果开发人员使用THUMNAIL_MID和THUMBNAIL,它应该抛出编译错误,以便开发人员知道只能使用一个THUMNAIL常量。如何限制某些类型的枚举?
不允许
ImageSize[] imageArray = {ImageSize.A4,ImageSize.THUMBNAIL_MID,ImageSize.THUMBNAIL} ;
允许
ImageSize[] imageArray = {ImageSize.A4,ImageSize.THUMBNAIL_MID} ;
ENUM代码
public enum ImageSize
{
THUMBNAIL(50, 50, "t"), PASSPORT(200, 200, "p"), SMALL(240, 135, "s"), MEDIUM(
480, 270, "m"), LARGE(960, 540, "l"), A4(800, 600, "a4"),THUMBNAIL_MID(120,155,"t");
/**
* The width of the image in pixels
*/
private final int width;
/**
* The height of the image in pixels
*/
private final int height;
/**
* The image size type
*/
private final String type;
ImageSize(int width, int height, String type)
{
this.width = width;
this.height = height;
this.type = type;
}
public int getWidth()
{
return width;
}
public int getHeight()
{
return height;
}
public String getType()
{
return type;
}
public static ImageSize getImageSizeByType(String type)
{
if (type != null)
{
if (type.equalsIgnoreCase(THUMBNAIL.getType()))
{
return THUMBNAIL;
}
if (type.equalsIgnoreCase(PASSPORT.getType()))
{
return PASSPORT;
}
if (type.equalsIgnoreCase(SMALL.getType()))
{
return SMALL;
}
if (type.equalsIgnoreCase(MEDIUM.getType()))
{
return MEDIUM;
}
if (type.equalsIgnoreCase(LARGE.getType()))
{
return LARGE;
}
if (type.equalsIgnoreCase(THUMBNAIL_MID.getType()))
{
return THUMBNAIL_MID;
}
}
return null;
}
}
}
我很确定你不能。但是你可以将你的代码封装在一个方法中,该方法返回一个带'THUMNAIL_MID'或'THUMBNAIL'的ImageSize'数组,并且像这样控制代码。 – rath