【问题标题】:How to compare objects belonging to different class using the IComparable interface如何使用 IComparable 接口比较属于不同类的对象
【发布时间】:2019-01-05 06:11:30
【问题描述】:

我正在尝试比较属于不同类的两个不同对象并使用IComparable 接口对它们进行排序。请在下面找到我的代码以及我可以改进的地方 我已经理解错误但不知道如何修复它。

public class Program : IComparable
{
    public static void Main(string[] args)
    {
        var newList = new List<Object>();
        newList.Add(new Program { carName = "mercedes benz", topSpeed = 160 });
        newList.Add(new SecondClass { carName = "BMW", topSpeed = 140 });
        newList.Sort();

        foreach (var list in newList)
        {
             Console.WriteLine(list.carName + " " + list.topSpeed);
             /* Getting an error on this line stating 'object' does not contain a definition for 'carName' and no extension method 'carName' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)*/
        }
    }

    public string carName { get; set; }
    public int topSpeed   { get; set; }

    public int CompareTo(Object obj)
    {
        Program classObject = (Program)obj;

        if (this.topSpeed > classObject.topSpeed)
            return 1;
        else if (this.topSpeed == classObject.topSpeed)
             return 0;
        else return -1;
    }
}

public class SecondClass
{
    public string carName { get; set; }
    public int topSpeed   { get; set; }
}

/* Edit: implemented abstract class however I am getting this "Unhandled Exception Message": System.InvalidCastException: Unable to cast object of type 'CSharpbasics.SecondClass' to type 'CSharpbasics.Program'.*/

public class SecondClass : Car { }

public abstract class Car : IComparable
{
    public string carName { get; set; }
    public int topSpeed   { get; set; }

    public int CompareTo(Object obj)
    {
        Program classObject = (Program)obj;

        if (this.topSpeed > classObject.topSpeed)
            return 1;
        else if (this.topSpeed == classObject.topSpeed)
             return 0;
        else return -1;
    }
}

public class Program : Car
{
    static void Main(string[] args)
    {
        var newList = new List<Car>();
        newList.Add(new Program { carName = "Wagon R", topSpeed = 160 });
        newList.Add(new SecondClass { carName = "Swift", topSpeed = 140 });

        newList.Sort();

        foreach (var list in newList) 
            Console.WriteLine($"{list.carName} {list.topSpeed});
    }
}

【问题讨论】:

  • 你有很多重复的代码。你真的应该考虑改变你的设计,不要重复相同的代码两次。

标签: c# interface icomparable


【解决方案1】:

您应该创建一个抽象类 Car,这两个类都继承自该类,并实现了 CompareTo 函数。父类不一定是抽象的,但在这种情况下我认为它会更合适。

【讨论】:

  • 做了同样的事情但是我得到这个未处理的异常消息 System.InvalidCastException: Unable to cast object of type 'CSharpbasics.SecondClass' to type 'CSharpbasics.Program'。
  • 你能编辑问题并展示你的做法吗?将其附加到问题的底部,不要删除已经存在的内容。
  • @ShravanRao 您应该更改 CompareTo 函数中的转换。当您应该投射到 Car 时,您正在尝试投射到 Program。
  • 我很糟糕,感谢您的帮助,现在工作正常,谢谢。
  • 没问题,记得把问题标记为已回答! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-24
相关资源
最近更新 更多