C# 运算符重载后置增量

发布于 2024-08-15 12:30:00 字数 490 浏览 4 评论 0原文

我正在编写一个日期类,并且在修复后增量方面遇到问题(前缀增量似乎很好)。

以下是示例代码:

public class date
{
    int year,
        month,
        day;

    public date(int d, int m, int y)
    {
        day = d;
        month = m;
        year = y;
    }

    static public date operator ++(date d)
    { 
        return d.Next(d);
    }
}

“Next(date d)”方法接受一个日期并返回明天的日期(为了简洁起见,我将其省略)。我对 C# 还太年轻,无法理解为什么前缀很好但后缀增量没有任何作用。但请记住,在 C++ 中,我们必须有两种方法,而不仅仅是一种 - 用于前缀和后缀增量。

编译时也没有错误或警告。

I'm coding a date class and am having trouble with the post-fix increment (the prefix increment seems fine).

Here is the sample code:

public class date
{
    int year,
        month,
        day;

    public date(int d, int m, int y)
    {
        day = d;
        month = m;
        year = y;
    }

    static public date operator ++(date d)
    { 
        return d.Next(d);
    }
}

The method "Next(date d)" takes a date and returns tomorrows date (I left it out for brevity). I'm to young in C# to understand why the prefix is fine but postfix increment does nothing. But remember in C++ we would have to have two methods instead of just one - for prefix and postfix increments.

Also no errors or warnings on compile.

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

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

发布评论

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

评论(5

拥有 2024-08-22 12:30:00

System.DateTime.AddDays

为自己保存一个史诗般的日期为主的头痛。

System.DateTime.AddDays

Save yourself an epic, date-based headache.

如痴如狂 2024-08-22 12:30:00

好吧,您还没有展示 Next 方法,这会有点方便......特别是展示为什么它需要将 date 作为参数。我的猜测是您的 Next 方法有缺陷。

您也没有显示后增量失败的示例。下面是一个简化的示例,表明它确实有效:

using System;

public class Date
{
    int year, month, day;

    public Date(int d, int m, int y)
    {
        day = d;
        month = m;
        year = y;
    }

    public static Date operator ++(Date d)
    { 
        return d.Next();
    }

    private Date Next()
    {
        // Just a toy implementation, obviously
        return new Date(day + 1, month, year);
    }

    static void Main()
    {
        Date x = new Date(1, 2, 3);
        x++;
        Console.WriteLine(x.day); // Prints 2
    }
}

注意它如何打印 2,表明日期已增加(或者更确切地说,x 现在指的是Date 具有递增的日期值)。

就我个人而言,我不认为我会为 Date 类引入 ++ 运算符,但没关系。我还建议构造函数应该是年/月/日而不是日/月/年;这是更传统的方法,并且更适合您希望通过更多参数获得更高精度的情况。

Well you haven't shown the Next method, which would be kinda handy... in particular showing why it needs to take a date as an argument. My guess is that your Next method is flawed.

You also haven't shown an example of it failing for postincrement. Here's a simplified example which shows that it does work:

using System;

public class Date
{
    int year, month, day;

    public Date(int d, int m, int y)
    {
        day = d;
        month = m;
        year = y;
    }

    public static Date operator ++(Date d)
    { 
        return d.Next();
    }

    private Date Next()
    {
        // Just a toy implementation, obviously
        return new Date(day + 1, month, year);
    }

    static void Main()
    {
        Date x = new Date(1, 2, 3);
        x++;
        Console.WriteLine(x.day); // Prints 2
    }
}

Note how it prints 2, showing that the day has been incremented (or rather, x now refers to a new instance of Date which has an incremented day value).

Personally I don't think I'd introduce a ++ operator for a Date class anyway, but never mind. I'd also suggest that the constructor should be year/month/day rather than day/month/year; that's more conventional, and fits in better with situations where you want to allow more precision with more parameters.

怀中猫帐中妖 2024-08-22 12:30:00

乔恩谢谢,你说得完全正确,让我附加类中缺少的 next() 方法:

    public date Next(date d)
    {
        if (!d.valid()) return new date();
        date ndat = new date((d.Day() + 1), d.Month(), d.Year());
        if (ndat.valid()) return ndat;
        ndat = new date(1, (d.Month() + 1), d.Year());
        if (ndat.valid()) return ndat;
        ndat = new date(1, 1, (d.Year() + 1));
        return ndat;
    }    

由于它使用 valid() 我也会附加它:

    public bool valid()
    {
        // This method will check the given date is valid or not.
        // If the date is not valid then it will return the value false.
        if (year < 0) return false;
        if (month > 12 || month < 1) return false;
        if (day > 31 || day < 1) return false;
        if ((day == 31 && (month == 2 || month == 4 || month == 6 || month == 9 || month == 11)))
            return false;
        if (day == 30 && month == 2) return false;
        if (day == 29 && month == 2 && (year % 4) != 0) return false;
        if (day == 29 && month == 2 && (((year % 100) == 0) && ((year % 400) != 0))) return false;
        /* ASIDE. The duration of a solar year is slightly less than 365.25 days. Therefore,
        years that are evenly divisible by 100 are NOT leap years, unless they are also
        evenly divisible by 400, in which case they are leap years. */
        return true;
    }

Day()、Month() 和 Year() I认为是不言自明的,但如果需要的话请告诉我。我还有一个 previous() 方法,它与 next() 相反,我想在 -- decrement 方法中使用它。

现在在我的程序中,我有

class Program
{
    static void Main()
    {
        date today = new date(7,10,1985);
        date tomoz = new date();

        tomorrow = today++; 

        tomorrow.Print();  // prints "7/10/1985" i.e. doesn't increment      
        Console.Read();
    }
}

所以它实际上并没有失败,它只是打印今天的日期而不是明天的日期,但如果我使用 ++today 则可以正常工作。

关于 D/M/Y 的顺序,是的,我同意,通过更高频率的数据,我可以看到这如何改进事情,接下来我将继续解决这个问题。

Jon thanks, you're absolutely right let me attach the missing next() method that is inside the class:

    public date Next(date d)
    {
        if (!d.valid()) return new date();
        date ndat = new date((d.Day() + 1), d.Month(), d.Year());
        if (ndat.valid()) return ndat;
        ndat = new date(1, (d.Month() + 1), d.Year());
        if (ndat.valid()) return ndat;
        ndat = new date(1, 1, (d.Year() + 1));
        return ndat;
    }    

Since this uses valid() I'll attach this also:

    public bool valid()
    {
        // This method will check the given date is valid or not.
        // If the date is not valid then it will return the value false.
        if (year < 0) return false;
        if (month > 12 || month < 1) return false;
        if (day > 31 || day < 1) return false;
        if ((day == 31 && (month == 2 || month == 4 || month == 6 || month == 9 || month == 11)))
            return false;
        if (day == 30 && month == 2) return false;
        if (day == 29 && month == 2 && (year % 4) != 0) return false;
        if (day == 29 && month == 2 && (((year % 100) == 0) && ((year % 400) != 0))) return false;
        /* ASIDE. The duration of a solar year is slightly less than 365.25 days. Therefore,
        years that are evenly divisible by 100 are NOT leap years, unless they are also
        evenly divisible by 400, in which case they are leap years. */
        return true;
    }

the Day(), Month(), and Year() I think are self explanatory but let me know if they're needed. I also have a previous() method that does the opposite of next() which I want to use in the -- decrement method.

Now in my program, I have

class Program
{
    static void Main()
    {
        date today = new date(7,10,1985);
        date tomoz = new date();

        tomorrow = today++; 

        tomorrow.Print();  // prints "7/10/1985" i.e. doesn't increment      
        Console.Read();
    }
}

So it doesn't actually fail it just prints todays date instead of tomorrow's but works correctly if I had used ++today instead.

Regarding the order D/M/Y, yep I agree, with higher frequency data I can see how that improves things, I'll move on to fixing that next.

烟雨凡馨 2024-08-22 12:30:00
DateTime SchDate= DateTime.Now;
SchDate= SchDate.AddDays(1);

您可以添加的日期或月份/年份

DateTime SchDate= DateTime.Now;
SchDate= SchDate.AddDays(1);

what ever may be the day or month/yr you can add

明明#如月 2024-08-22 12:30:00

我还有一条附加评论,对于原始海报来说可能为时已晚,但可能对将来阅读的任何人都有用。

看看你的“有效”实现:

    if (day == 29 && month == 2 && (year % 4) != 0) return false; 
    if (day == 29 && month == 2 && (((year % 100) == 0) && ((year % 400) != 0))) return false; 

它将在 29/2/2100 失败,这是一个有效日期,但你的方法说它不是。

I have an additional comment that is probably too late for the original poster, but may be useful to anyone reading in the future.

Looking at your implementation of "valid":

    if (day == 29 && month == 2 && (year % 4) != 0) return false; 
    if (day == 29 && month == 2 && (((year % 100) == 0) && ((year % 400) != 0))) return false; 

It will fail on 29/2/2100, which is a valid date but your method says it's not.

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