0

我在我的名为 Picture 的代码中创建了 ref 类,该类中有类似的属性Bitmap^ Sytem::String path和其他属性。我想创建一个指示图片形状的新属性。

我想要三个不同的形状类,一个是正方形,一个是水平矩形,最后一个是垂直矩形。通过查看位图像素宽度和高度,我希望能够更改我的图片的属性以匹配三个类之一。问题是我不知道我会怎么做。举个例子,假设我有这个矩形:

 _____________________
|                     |
|                     |
|_____________________|

Picture->TemplateType = HORIZONTALRECTANGLE

到目前为止,我的图片类看起来像这样:

    public ref class Picture{
public: 
    System::String^ path;   
    BitMap^ image;  
    PosOnSlide *PositionAtributes;
    bool EmptyPic;
    bool PlacedOnSlide; 
};

我总是可以做到这一点

    public ref class Picture{
public: 
    System::String^ path;   
    BitMap^ image;  
    PosOnSlide *PositionAtributes;
    bool EmptyPic;
    bool PlacedOnSlide;
            int TemplateType // 0 = square, 1 = vertical, 2 = horrizontal   
};

但我认为#define 一个类或枚举,或者具有三个不同属性的东西,代码维护会更容易。

我有哪些选择?

4

1 回答 1

1

是的,你是对的,使用枚举可能是一个简单的解决方案:

enum class PictureType
{
    Square,
    HorizontalRectangle,
    VerticalRectangle
};

public ref class Picture
{
public: 
    System::String^ path;   
    BitMap^ image;  
    PosOnSlide *PositionAtributes;
    bool EmptyPic;
    bool PlacedOnSlide;
    PictureType Type;
};

int main(array<System::String ^> ^args)
{
    Picture^ picture = gcnew Picture();
    picture->Type = PictureType::Square;

    return 0;
}

但是您可能希望根据位图属性分离类型并生成不同的实例

public ref class Picture
{
public: 
    System::String^ path;   
    BitMap^ image;  
    PosOnSlide *PositionAtributes;
    bool EmptyPic;
    bool PlacedOnSlide;
    Picture(System::String^ path, BitMap^ bitmap)
    {
        this->path = path;
        this->image = bitmap;
    }
};

public ref class Square : Picture
{
public:
    Square(System::String^ path, BitMap^ bitmap)
        : Picture(path, bitmap)
    {
    }
};

public ref class HorizontalRectangle : Picture
{
public:
    HorizontalRectangle(System::String^ path, BitMap^ bitmap)
        : Picture(path, bitmap)
    {
    }
};

public ref class VerticalRectangle : Picture
{
public:
    VerticalRectangle(System::String^ path, BitMap^ bitmap)
        : Picture(path, bitmap)
    {
    }
};

public ref class PictureFactory
{
public:
    static Picture^ GetPicture(System::String^ path, BitMap^ bitmap)
    {
        Picture^ picture = nullptr;
        if (bitmap->Height == bitmap->Width)
        {
            picture = gcnew Square(path, bitmap);
        }
        else if (bitmap->Height < bitmap->Width)
        {
            picture = gcnew HorizontalRectangle(path, bitmap);
        }
        else if (bitmap->Height > bitmap->Width)
        {
            picture = gcnew VerticalRectangle(path, bitmap);
        }

        return picture;
    }
};

int main(array<System::String ^> ^args)
{
    Picture^ square = PictureFactory::GetPicture("image.jpg", gcnew BitMap(100, 100));
    Picture^ hrect = PictureFactory::GetPicture("image.jpg", gcnew BitMap(100, 10));
    Picture^ vrect = PictureFactory::GetPicture("image.jpg", gcnew BitMap(10, 100));

    System::Console::WriteLine(square->GetType());
    System::Console::WriteLine(hrect->GetType());
    System::Console::WriteLine(vrect->GetType());

    return 0;
}

这取决于您如何使用这些对象,越简单越好。:)

于 2013-06-20T16:55:53.650 回答