简体   繁体   English

运算符重载?

[英]Operator overloading?

I've made myself a rss reader that keeps me up to date and informs me on new shows, or atleast thats the thought behind. 我已经让自己成为了一个RSS阅读器,它让我保持最新状态,并告诉我新的节目,或者至少是这背后的想法。

I've made a struct "SeasonEpisode" that hold two ints (season+episode) and a override ToString function. 我制作了一个结构“SeasonEpisode”,它包含两个整数(季节+剧集)和一个覆盖ToString函数。

I store the latest watched locally and i then read whats the newest is from the rss. 我存储了本地最新观看的内容,然后我阅读了最新版本来自rss的内容。 But how could I compare SeasonEpisodes? 但是我如何比较SeasonEpisodes呢? right now I take each of the ints and compare them 现在我采取每个整数并比较它们

if( se1.Season >= se2.Season )
    if( se1.Episode > se2.Episode || se1.Season > se2.Season )
        // new episode!

What i really want is 我真正想要的是

if( se1 > se2 )
    // new episode

Could i get any help please? 我可以得到任何帮助吗?

There are two ways: 有两种方法:

  1. Implement IComparable<T> and use CompareTo 实现IComparable<T>并使用CompareTo
  2. Overload the greater and less than operators 超载大于和小于运营商

I suggest, you use both ways: 我建议你用两种方式:

public class SeasonEpisode : IComparable<SeasonEpisode>
{
    public int CompareTo(SeasonEpisode other)
    {
        if(other == null)
            return 1;
        if(Season == other.Season)
        {
            if(Episode == other.Episode)
                return 0;
            else if(Episode < other.Episode)
                return -1;
            else
                return 1;
        }
        else if(Season < other.Season) 
            return -1;
        else
            return 1;
    }

    public static bool operator <(SeasonEpisode e1, SeasonEpisode e2) 
    {
        return e1.CompareTo(e2) < 0;
    }

    public static bool operator >(SeasonEpisode e1, SeasonEpisode e2) 
    {
        return e1.CompareTo(e2) > 0;
    }
}

As i tripped over a NullReferenceException , here's an improvement (well this may be subjective ;-)) to Daniel Hilgarth's answer . 当我绊倒NullReferenceException ,这是对Daniel Hilgarth的回答的改进(这可能是主观的;-))。

The only change is that it handles null s in case the first argument to the > or < operator is null: 唯一的变化是,如果><运算符的第一个参数为null ,它会处理null:

public class SeasonEpisode : IComparable<SeasonEpisode>
{
    private static int Compare(SeasonEpisode e1, SeasonEpisode e2)
    {
        if (e1 == null && e2 == null)
            return 0;
        else if (e1 == null)
            return -1;
        else if (e2 == null)
            return 1;

        if(e1.Season == e2.Season)
        {
            if(e1.Episode == e2.Episode)
                return 0;
            else if(e1.Episode < e2.Episode)
                return -1;
            else
                return 1;
        }
        else if(e1.Season < e2.Season) 
            return -1;
        else
            return 1;
    }    

    public int CompareTo(SeasonEpisode other)
    {
        return Compare(this, other);
    }

    public static bool operator <(SeasonEpisode e1, SeasonEpisode e2) 
    {
        return Compare(e1, e2) < 0;
    }

    public static bool operator >(SeasonEpisode e1, SeasonEpisode e2) 
    {
        return Compare(e1, e2) > 0;
    }
}

You can implement the IComparer<T> interface 您可以实现IComparer<T>接口

Defines a method that a type implements to compare two objects. 定义类型实现比较两个对象的方法。

You can implement IComparable if you want a class to be comparable to another instance of that class. 如果希望类与该类的另一个实例相当 ,则可以实现IComparable Which is probably what you want, in this case. 在这种情况下,这可能是你想要的。

Implement IComparer if you need a class that compares two objects . 如果需要一个比较两个对象的类,请实现IComparer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM