对象比较(多个关卡)

本文关键字:比较 对象 | 更新日期: 2023-09-27 18:09:06

我想创建具有5个属性的对象,每个属性有2个属性。之后,我比较对象,如果他们是相同的,他们将被分组在同一类别。

代码如下:

Item.cs

public class Item
{
    public Item()
    {
    }
    public SortProperty SortPropA { get; set; }
    public SortProperty SortPropB { get; set; }
    public SortProperty SortPropC { get; set; }
    public SortProperty SortPropD { get; set; }
    public SortProperty SortPropE { get; set; }
    public string Name { get; set; }
    public string Desc { get; set; }
}

SortProperty.cs

public class SortProperty : IEquatable<SortProperty>
{
    public string PartName { get; set; }
    public string GroupabilityID { get; set; }
    public SortProperty()
    {
    }
    public override int GetHashCode()
    {
        int hash = 19;
        hash = hash * 31 + (GroupabilityID == null ? 0 : GroupabilityID.GetHashCode());
        hash = hash * 31 + (PartName == null ? 0 : PartName.GetHashCode());
        return hash;
    }
    public bool Equals(SortProperty obj)
    {
        return (obj == null) ?
           false : (GroupabilityID == obj.GroupabilityID) || (PartName == obj.PartName);
    }
    public override bool Equals(Object obj)
    {
        SortProperty itemobj = obj as SortProperty;
        return itemobj == null ? false : Equals(itemobj);
    }
}

Program.cs(测试编码的主类)

class Program
{
    static void Main(string[] args)
    {
        Item objA = new Item();
        Item objB = new Item();
        // ------ Object A
        objA.Name = "Card1";
        objA.Desc = "Product Test A";
        //Property A
        objA.SortPropA = new SortProperty();
        objA.SortPropA.PartName = "Plastic A";
        objA.SortPropA.GroupabilityID = "A1";
        //Property B
        objA.SortPropB = new SortProperty();
        objA.SortPropB.PartName = "Color Green";
        objA.SortPropB.GroupabilityID = "B2";
        //Property C
        objA.SortPropC = new SortProperty();
        objA.SortPropC.PartName = "Visa";
        objA.SortPropC.GroupabilityID = "C1";
        // ------ Object B
        objB.Name = "Card2";
        objB.Desc = "Product Test B";
        //Property A
        objB.SortPropA = new SortProperty();
        objB.SortPropA.PartName = "Plastic B";
        objB.SortPropA.GroupabilityID = "A2";
        //Property B
        objB.SortPropB = new SortProperty();
        objB.SortPropB.PartName = "Color Lime";
        objB.SortPropB.GroupabilityID = "B1";
        //Property C
        objB.SortPropC = new SortProperty();
        objB.SortPropC.PartName = "Visa";
        objB.SortPropC.GroupabilityID = "C1";
        bool isEqual = objA.Equals(objB);
        if (isEqual == true)
            Console.WriteLine("Is same");
        else
            Console.WriteLine("Is different");
        Console.ReadKey();
    }
    }

结果应该返回true,因为在objAobjB (SortPropc)之间有相同的属性,但它返回false

我相信我错过了一些逻辑部分,我坐在椅子上4个小时,但无法解决它。有人能解决吗?

对象比较(多个关卡)

结果应该返回true,因为objA和objB (sortproc)之间有相同的属性,但它返回false。

你只是没有实现它。再次阅读您的代码,并尝试找到您实际比较两个Item实例的部分。没有。

你应该在你的Item类上实现EqualsGetHashCode方法,就像这样:

public override bool Equals(Object obj)
{
    var o = (Item)obj;
    // Note: not error checking :-)
    return SortPropA.Equals(o.SortPropA) || 
           SortPropB.Equals(o.SortPropB) || 
           SortPropC.Equals(o.SortPropC) || 
           SortPropD.Equals(o.SortPropD) || 
           SortPropE.Equals(o.SortPropE);
}

或者创建一个实现IEqualityComparer<Item>的类来处理这个需求