C#运算符重载修复后增量

时间:2009-12-24 11:27:11

标签: c# operators operator-overloading postfix-operator

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

以下是示例代码:

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

方法“下一个(日期d)”取一个日期并返回明天日期(我为了简洁而将其留下)。我要年轻的C#来理解为什么前缀很好但后缀增量什么也没做。但请记住,在C ++中,我们必须有两个方法而不是一个 - 用于前缀和后缀增量。

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

5 个答案:

答案 0 :(得分:6)

System.DateTime.AddDays

为自己保留一个史诗般的,基于日期的头痛。

答案 1 :(得分:4)

嗯,你没有展示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课程引入++运算符,但没关系。我还建议构造函数应该是年/月/日而不是日/月/年;这是更传统的,并且在你希望允许更多精度和更多参数的情况下更适合。

答案 2 :(得分:0)

Jon谢谢,你是绝对正确的,让我附上课堂内缺少的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()我认为是自解释的,但如果需要,请告诉我。我也有一个previous()方法与next()相反,我想在 - 减量方法中使用它。

现在在我的计划中,我有

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

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

关于订单D / M / Y,是的,我同意,有了更高频率的数据,我可以看到这是如何改善的,我将继续修复下一步。

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

我还有一个额外的评论,对于原版海报可能为时已晚,但对将来读书的人可能会有用。

查看“有效”的实施:

    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失败,这是一个有效的日期,但你的方法说它不是。

相关问题