【问题标题】:C# Operator Overloading post-fix incrementC# 运算符重载后修复增量
【发布时间】:2010-12-29 19:14:25
【问题描述】:

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

这里是示例代码:

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++ 中,我们必须有两种方法,而不是只有一种 - 用于前缀和后缀增量。

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

【问题讨论】:

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


    【解决方案1】:

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

    查看您对“有效”的实现:

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

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

    【讨论】:

      【解决方案2】:
      DateTime SchDate= DateTime.Now;
      SchDate= SchDate.AddDays(1);
      

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

      【讨论】:

        【解决方案3】:

        System.DateTime.AddDays

        为自己省去一个史诗般的、基于日期的头痛。

        【讨论】:

          【解决方案4】:

          谢谢乔恩,你说得对,让我附上类中缺少的 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() 方法,它与我想在 --decrement 方法中使用的 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();
              }
          }
          

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

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

          【讨论】:

          • 呃,当然可以?您将明天分配给今天的值,然后将其递增。
          • 你不会期望明天是 2 之后:int today = 1; int tomorrow = today++;
          • 我不得不同意您的 Next 方法确实不应该接受日期参数——它应该只在关联实例之后给出下一个日期。要么,要么你可以把它变成一个静态方法。
          【解决方案5】:

          好吧,您还没有展示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 类引入 ++ 运算符,但没关系。我还建议构造函数应该是年/月/日而不是日/月/年;这更传统,更适合您希望通过更多参数实现更高精度的情况。

          【讨论】:

            猜你喜欢
            • 2011-12-21
            • 2010-10-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-02-02
            • 2012-04-13
            相关资源
            最近更新 更多