1

在查看 System.pas 时,TDate 和 TDateTime 定义如下:

  TDateTime = type Double;

  TDate = type TDateTime;
  TTime = type TDateTime;

显然 TDate 和 TDateTime 是相同的。

我只是在使用 TDate 和 TDateTime 时遇到了困难,因为我希望 TDate 只包含日期部分而不包含时间部分。

现在我想知道:这背后的意义是什么?当我将变量声明为 TDate 时,它​​应该包含一个日期,而不是一个 dat 和一个时间值。

4

1 回答 1

8

They are not the same. If the declarations had been

TDate = TDateTime;
TTime = TDateTime;

they had been the same. With the additional type, although they are still technically the same, i.e., they are still doubles, they can be told apart. This, for instance, makes it possible to use different property editors in the Object Inspector for the two types (a date picker and a time picker, respectively), while you may use a 'date-time' picker for TDateTime.

In addition, even if it weren't for this, it might be good to use different 'aliases' for different purposes. This might make your source code easier to understand. For instance, if you do

var
  StartTime: TDate;

then you know that StartTime contains only information about the date, and not the time (unless you abuse the norms).

于 2012-08-29T08:58:52.303 回答