是的,你是对的,使用枚举可能是一个简单的解决方案:
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;
}
这取决于您如何使用这些对象,越简单越好。:)