[英]How do you compare objects with different properties in run-time?
My code structure is something like this我的代码结构是这样的
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...
I am trying to figure out the right logic in how to solve this problem so the picture is a simple representation of what I have.我试图找出解决这个问题的正确逻辑,所以这张图片是我所拥有的简单表示。
I have a object structure of something like in the picture where they're all inheriting the level above them.我有一个类似于图片中的 object 结构,它们都继承了它们之上的级别。 They all have properties of its type.
它们都具有其类型的属性。 They would all have ObjectType, and the 2nd level has DrawingType, and the 3rd level has either ShapeType, LineType, or PointType...
它们都会有 ObjectType,第 2 层有 DrawingType,第 3 层有 ShapeType、LineType 或 PointType……
So for example, square would have例如,正方形会有
square {
ObjectType = "drawing";
DrawingType = "shapes";
ShapeType = "square"
}
However, vertical would have different properties但是,垂直将具有不同的属性
vertical {
ObjectType = "drawing";
DrawingType = "lines";
LineType = "vertical"
}
Now my problem is, lets say I wanted a user to select something based on their input, how would i do it?现在我的问题是,假设我想要一个用户 select 基于他们的输入,我该怎么做? Like lets say the user typed "square", how would I select square?
就像假设用户输入“正方形”一样,我将如何使用 select 正方形? It would be easy if the properties were the same cause I could just compare them.
如果属性相同,那将很容易,因为我可以比较它们。 But how would I do it with a structure like this?
但是我该如何使用这样的结构呢?
As these all classes are derived from one base class hence all the child classes contains the property coming from parents.由于这些所有类都是从一个基础 class 派生的,因此所有子类都包含来自父类的属性。 Object level comparison will lead you comparison of one Drawing object to another as all the objects are derived from there only.
Object 级别比较将引导您将一张图纸 object 与另一张图纸进行比较,因为所有对象都仅从那里派生。 Hence i have implemented IComparer to get your desired result ->
因此,我实施了 IComparer 以获得您想要的结果->
Here is the code ->>这是代码->>
Main Class :主要 Class :
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);
Other classes :其他类:
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";
}
Hope this will work for you.希望这对你有用。 please write back with you code snippet specifically not working.
请写回您的代码片段,具体不起作用。 Cheers
干杯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.