运算符重载?

发布于 2024-11-01 16:41:11 字数 441 浏览 4 评论 0原文

我让自己成为了一个 RSS 阅读器,它可以让我了解最新情况并通知我新节目,或者至少这就是背后的想法。

我创建了一个结构体“SeasonEpisode”,其中包含两个整数(季节+剧集)和一个覆盖 ToString 函数。

我将最新观看的内容存储在本地,然后从 rss 中读取最新内容。但我如何比较季集呢?现在我获取每个整数并比较它们

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

我真正想要的是

if( se1 > se2 )
    // new episode

我可以获得任何帮助吗?

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.

I've made a struct "SeasonEpisode" that hold two ints (season+episode) and a override ToString function.

I store the latest watched locally and i then read whats the newest is from the rss. But how could I compare 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷…雨湿花 2024-11-08 16:41:11

有两种方法:

  1. 实施 IComparable 并使用 CompareTo
  2. 重载大于和小于运算符

我建议,您使用两种方式:

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;
    }
}

There are two ways:

  1. Implement IComparable<T> and use 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;
    }
}
-残月青衣踏尘吟 2024-11-08 16:41:11

当我被 NullReferenceException 绊倒时,这是对 Daniel Hilgarth 的改进(这可能是主观的;-))回答

唯一的变化是,如果 >< 运算符的第一个参数为 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;
    }
}

As i tripped over a NullReferenceException, here's an improvement (well this may be subjective ;-)) to Daniel Hilgarth's answer.

The only change is that it handles nulls in case the first argument to the > or < operator is 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;
    }
}
挽你眉间 2024-11-08 16:41:11

您可以实现 IComparer界面

定义类型实现的用于比较两个对象的方法。

如果您希望一个类与该类的另一个实例具有可比性,您可以实现 IComparable。在这种情况下,这可能就是您想要的。

如果您需要一个比较两个对象的类,请实现IComparer

You can implement the IComparer<T> interface

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. Which is probably what you want, in this case.

Implement IComparer if you need a class that compares two objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文