枚举类型未设定范围,相比于enum,要修改成“enum class”
时间: 2023-09-18 17:09:16 浏览: 331
是的,您是正确的。在C++中,枚举类型有两种:普通枚举和强类型枚举(也称为枚举类)。普通枚举中的枚举值是全局可见的,而枚举类中的枚举值是在枚举类的作用域内可见的。因此,使用枚举类可以避免名称冲突并提高代码的安全性。同时,枚举类还可以指定其底层类型,从而限制其值的范围。
要将普通枚举修改为枚举类,只需在enum关键字后添加class关键字即可,如下所示:
```
enum class Color { Red, Green, Blue };
```
这样定义的枚举类型Color中的枚举值只在Color作用域内可见,并且不会与其他命名空间中的枚举值冲突。如果需要指定底层类型,可以使用冒号(:)后跟类型名称的方式来定义,例如:
```
enum class Color : uint8_t { Red, Green, Blue };
```
这样定义的枚举类型Color中的枚举值的底层类型为uint8_t,可以避免超出范围的错误。
相关问题
枚举类型未设定范围,相比于enum
class更加灵活,可以自定义枚举类型的值。而enum类型的值是固定的,无法修改。
在class中,我们可以使用常量或者静态变量来定义枚举类型,同时可以定义方法和属性来扩展枚举类型的功能。
例如,我们可以使用以下代码来定义一个月份的枚举类型:
```
public class Month {
public static final int JANUARY = 1;
public static final int FEBRUARY = 2;
public static final int MARCH = 3;
//...
public static final int DECEMBER = 12;
}
```
在使用时,可以直接调用Month类中定义的常量来表示月份。
而如果使用enum类型来定义月份,则需要使用以下代码:
```
public enum Month {
JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE,
JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER
}
```
此时,Month枚举类型的值只能是JANUARY、FEBRUARY等12个月份中的一个,无法自定义。
实现类Img,参考形式如下: Class Img { Img(){} void readImg(string file);//从参数file指定的路径读取图像文件,有合理的函数体即可 void writeImg(string file);//将图像数据写出到参数file指定的路径,有合理的函数体即可 enum IMG_STORE_TYPE{GRAY,GRAY_A,RGB,RGB_A;};//图像的存储形式有四种: 灰度通道,灰度通道+透明度通道,RGB通道,RGB通道+透明度通道 IMG_STORE_TYPE store_type;//根据readImg的信息设定具体的store_type。 short[] pixel_data;存储的像素颜色数据 Img UnaryOperation(ShortUnaryOperator unary_op); Img BinaryOperation(Img img, ShortBinaryOperator binary_op); } 实现方法UnaryOperation和BinaryOperation,根据上述图像运算关系的定义,对pixel_data中的数据进行遍历运算,参数img是另一个参与运算的相同大小的图像,binary_op是具体运算的形式,对UnaryOperation实现反色操作(使用颜色的最大值255减去当前像素值得到新的像素值),对BinaryOperation实现加减两种运算形式(减法结果小于0的情况,截断到0)。
以下是Img类的实现代码:
```java
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.function.ShortBinaryOperator;
import java.util.function.ShortUnaryOperator;
import javax.imageio.ImageIO;
public class Img {
private static final int MAX_COLOR_VALUE = 255;
private static final int ALPHA_MASK = 0xff000000;
private static final int RED_MASK = 0xff0000;
private static final int GREEN_MASK = 0xff00;
private static final int BLUE_MASK = 0xff;
private int width;
private int height;
private IMG_STORE_TYPE storeType;
private short[] pixelData;
public Img() {}
public void readImg(String file) {
try {
BufferedImage image = ImageIO.read(new File(file));
width = image.getWidth();
height = image.getHeight();
storeType = getImgStoreType(image);
pixelData = new short[width * height * getNumChannels(storeType)];
switch (storeType) {
case GRAY:
readGrayImage(image);
break;
case GRAY_A:
readGrayAlphaImage(image);
break;
case RGB:
readRgbImage(image);
break;
case RGB_A:
readRgbAlphaImage(image);
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void writeImg(String file) {
BufferedImage image = new BufferedImage(width, height, getImageType(storeType));
int[] pixels = new int[width * height];
for (int i = 0; i < pixels.length; i++) {
pixels[i] = getPixelValue(i);
}
image.setRGB(0, 0, width, height, pixels, 0, width);
try {
ImageIO.write(image, "png", new File(file));
} catch (IOException e) {
e.printStackTrace();
}
}
public Img UnaryOperation(ShortUnaryOperator unary_op) {
Img result = new Img();
result.width = width;
result.height = height;
result.storeType = storeType;
result.pixelData = new short[pixelData.length];
for (int i = 0; i < pixelData.length; i++) {
result.pixelData[i] = (short) unary_op.applyAsShort(pixelData[i]);
}
return result;
}
public Img BinaryOperation(Img img, ShortBinaryOperator binary_op) {
if (width != img.width || height != img.height || storeType != img.storeType) {
throw new IllegalArgumentException("The two images must have the same dimensions and store type.");
}
Img result = new Img();
result.width = width;
result.height = height;
result.storeType = storeType;
result.pixelData = new short[pixelData.length];
for (int i = 0; i < pixelData.length; i++) {
int value = binary_op.applyAsShort(pixelData[i], img.pixelData[i]);
result.pixelData[i] = (short) Math.max(0, Math.min(value, MAX_COLOR_VALUE));
}
return result;
}
private void readGrayImage(BufferedImage image) {
int[] pixels = image.getRGB(0, 0, width, height, null, 0, width);
for (int i = 0; i < pixels.length; i++) {
int gray = (pixels[i] >> 16) & 0xff;
pixelData[i] = (short) gray;
}
}
private void readGrayAlphaImage(BufferedImage image) {
int[] pixels = image.getRGB(0, 0, width, height, null, 0, width);
for (int i = 0; i < pixels.length; i++) {
int gray = (pixels[i] >> 16) & 0xff;
int alpha = (pixels[i] >> 24) & 0xff;
pixelData[2 * i] = (short) gray;
pixelData[2 * i + 1] = (short) alpha;
}
}
private void readRgbImage(BufferedImage image) {
int[] pixels = image.getRGB(0, 0, width, height, null, 0, width);
for (int i = 0; i < pixels.length; i++) {
int red = (pixels[i] >> 16) & 0xff;
int green = (pixels[i] >> 8) & 0xff;
int blue = pixels[i] & 0xff;
pixelData[3 * i] = (short) red;
pixelData[3 * i + 1] = (short) green;
pixelData[3 * i + 2] = (short) blue;
}
}
private void readRgbAlphaImage(BufferedImage image) {
int[] pixels = image.getRGB(0, 0, width, height, null, 0, width);
for (int i = 0; i < pixels.length; i++) {
int red = (pixels[i] >> 16) & 0xff;
int green = (pixels[i] >> 8) & 0xff;
int blue = pixels[i] & 0xff;
int alpha = (pixels[i] >> 24) & 0xff;
pixelData[4 * i] = (short) red;
pixelData[4 * i + 1] = (short) green;
pixelData[4 * i + 2] = (short) blue;
pixelData[4 * i + 3] = (short) alpha;
}
}
private int getPixelValue(int index) {
switch (storeType) {
case GRAY:
return (pixelData[index] << 16) | (pixelData[index] << 8) | pixelData[index] | ALPHA_MASK;
case GRAY_A:
return (pixelData[2 * index] << 16) | (pixelData[2 * index] << 8) | pixelData[2 * index] |
(pixelData[2 * index + 1] << 24);
case RGB:
return (pixelData[3 * index] << 16) | (pixelData[3 * index + 1] << 8) | pixelData[3 * index] | ALPHA_MASK;
case RGB_A:
return (pixelData[4 * index] << 16) | (pixelData[4 * index + 1] << 8) | pixelData[4 * index] |
(pixelData[4 * index + 3] << 24);
default:
return 0;
}
}
private IMG_STORE_TYPE getImgStoreType(BufferedImage image) {
switch (image.getType()) {
case BufferedImage.TYPE_BYTE_GRAY:
return IMG_STORE_TYPE.GRAY;
case BufferedImage.TYPE_BYTE_GRAY_ALPHA:
return IMG_STORE_TYPE.GRAY_A;
case BufferedImage.TYPE_3BYTE_BGR:
return IMG_STORE_TYPE.RGB;
case BufferedImage.TYPE_4BYTE_ABGR:
return IMG_STORE_TYPE.RGB_A;
default:
throw new IllegalArgumentException("Unsupported image type.");
}
}
private int getImageType(IMG_STORE_TYPE storeType) {
switch (storeType) {
case GRAY:
return BufferedImage.TYPE_BYTE_GRAY;
case GRAY_A:
return BufferedImage.TYPE_BYTE_GRAY_ALPHA;
case RGB:
return BufferedImage.TYPE_3BYTE_BGR;
case RGB_A:
return BufferedImage.TYPE_4BYTE_ABGR;
default:
return BufferedImage.TYPE_INT_ARGB;
}
}
private int getNumChannels(IMG_STORE_TYPE storeType) {
switch (storeType) {
case GRAY:
case GRAY_A:
return 1;
case RGB:
return 3;
case RGB_A:
return 4;
default:
return 0;
}
}
public Img UnaryOperation() {
return UnaryOperation(pixel -> MAX_COLOR_VALUE - pixel);
}
public Img BinaryOperation(Img img, IMG_OPERATION_TYPE operationType) {
ShortBinaryOperator binary_op;
switch (operationType) {
case ADD:
binary_op = (p1, p2) -> p1 + p2;
break;
case SUBTRACT:
binary_op = (p1, p2) -> p1 - p2;
break;
default:
throw new IllegalArgumentException("Unsupported image operation type.");
}
return BinaryOperation(img, binary_op);
}
enum IMG_STORE_TYPE {
GRAY,
GRAY_A,
RGB,
RGB_A
}
enum IMG_OPERATION_TYPE {
ADD,
SUBTRACT
}
}
```
在Img类中实现了readImg和writeImg方法,用于读取和写出图像文件。其中,readImg方法根据图像的存储形式调用不同的读取方法,并将读取的像素颜色数据存储在pixelData数组中。writeImg方法则将pixelData数组中的像素颜色数据写出到图像文件中。
实现了UnaryOperation和BinaryOperation方法,对pixelData中的数据进行遍历运算。对于UnaryOperation,实现了反色操作;对于BinaryOperation,实现了加减两种运算形式,对于减法结果小于0的情况,截断到0。
此外,Img类还定义了IMG_STORE_TYPE枚举类型,表示图像的存储形式,以及IMG_OPERATION_TYPE枚举类型,表示图像的运算类型。在实现BinaryOperation方法时,根据IMG_OPERATION_TYPE选择具体的运算操作。
阅读全文