c# - 您如何在运行时比较具有不同属性的对象?
问题描述
我的代码结构是这样的
class Drawing()
{
ObjectType = "Drawing";
}
class Shape() : Drawing()
{
DrawingType = "Shape";
}
class square() : Shape()
{
ShapeType = "square";
}
class circle() : Shape()
{
ShapeType = "circle";
}
class triangle() : Shape()
{
ShapeType = "triangle";
}
class lines() : Drawing()
{
DrawingType = "lines";
}
class horizontal() : lines()
{
linesType = "horizontal";
}
class vertical() : lines()
{
linesType = "vertical";
}
etc...
我试图找出解决这个问题的正确逻辑,所以这张图片是我所拥有的简单表示。
我有一个类似于图片中的对象结构,它们都继承了它们上面的级别。它们都具有其类型的属性。它们都会有 ObjectType,第 2 层有 DrawingType,第 3 层有 ShapeType、LineType 或 PointType……
例如,正方形会有
square {
ObjectType = "drawing";
DrawingType = "shapes";
ShapeType = "square"
}
但是,垂直将具有不同的属性
vertical {
ObjectType = "drawing";
DrawingType = "lines";
LineType = "vertical"
}
现在我的问题是,假设我希望用户根据他们的输入选择一些东西,我该怎么做?就像假设用户输入“正方形”一样,我将如何选择正方形?如果属性相同,那将很容易,因为我可以比较它们。但是我该如何使用这样的结构呢?
解决方案
由于这些所有类都派生自一个基类,因此所有子类都包含来自父类的属性。对象级别比较将引导您将一个绘图对象与另一个对象进行比较,因为所有对象都仅从那里派生。因此,我实施了 IComparer 以获得您想要的结果->
这是代码->>
主要课程:
Drawing drawing1 = new Drawing();
Drawing drawing2 = new Drawing();
drawing2.ObjectType = "newdrawing";
Shape shape = new Shape();
triangle triangle1 = new triangle();
triangle triangle2 = new triangle();
triangle2.ShapeType = "another";
//all -1 is not equals and 0 is equals
int result1 = drawing1.Compare(drawing1, drawing2);
int result2 = drawing1.Compare(drawing1, drawing1);
int result3 = drawing1.Compare(drawing1, shape);
int result4 = shape.Compare(shape, triangle1);
int result5 = triangle1.Compare(triangle1, triangle2);
其他类:
public class Drawing : IComparer
{
public string ObjectType { get; set; } = "Drawing";
public int Compare(object x, object y)
{
//returning 0 is equals
if (x.GetType().Name == y.GetType().Name)
{
switch (x.GetType().Name)
{
case "Drawing":
return ((Drawing)x).ObjectType == ((Drawing)y).ObjectType ? 0 : -1;
case "Shape":
return ((Shape)x).DrawingType == ((Shape)y).DrawingType ? 0 : -1;
case "square":
return ((square)x).ShapeType == ((square)y).ShapeType ? 0 : -1;
case "triangle":
return ((triangle)x).ShapeType == ((triangle)y).ShapeType ? 0 : -1;
default:
return -1; // Not equal
}
}
return -1; // Not equal
}
}
class Shape : Drawing
{
public string DrawingType { get; set; } = "Shape";
}
class square : Shape
{
public string ShapeType { get; set; } = "square";
}
class circle : Shape
{
public string ShapeType { get; set; } = "circle";
}
class triangle : Shape
{
public string ShapeType { get; set; } = "triangle";
}
class lines : Drawing
{
public string DrawingType { get; set; } = "lines";
}
class horizontal : lines
{
public string linesType { get; set; } = "horizontal";
}
class vertical : lines
{
public string linesType { get; set; } = "vertical";
}
希望这对你有用。请写回您的代码片段,具体不起作用。干杯
推荐阅读
- flutter - error: The function 'MyApp' isn't defined. (undefined_function at [i_am_a_rich_man] test\widget_test.dart:16)
- javascript - 我如何知道是否有对给定实例的引用?
- python - 如果没有输入python清除控制台
- laravel - laravel:notification 重命名数据库并作用于模型
- javascript - 跨度不显示
- javascript - 在 Firefox 中更改其左/左边距时阻止抽搐
- c++ - C++:将 lambda 指针作为函数指针传递
- arrays - 扩展 RegexMatch 以针对整个单元格范围进行测试 - Google 表格
- c# - 静态 HttpClient 需要来自 app.settings 的 URI
- java - spring boot junit 不同的配置文件不起作用