Unit2unit Unit2;interface
uses
  DateUtils, SysUtils;type
  TMyClass = class
  FDate : TDateTime;
    private
    procedure SetDay(const Value: Integer);
    procedure SetMonth(const Value: Integer);
    procedure SetYear(const Value: Integer);
    function GetDay: Integer;
    function GetMonth: Integer;
    function GetYear: Integer;
    public
    procedure SetValue(d, m ,y : Integer); overload;
    property OnDay : Integer read GetDay write SetDay;
    property OnMonth : Integer read GetMonth write SetMonth;
    property OnYear : Integer read GetYear write SetYear;
  end;implementation{ TMyClass }
function TMyClass.GetDay: Integer;
begin
  Result := DayOf(FDate);
end;function TMyClass.GetMonth: Integer;
begin
  Result := MonthOf(FDate);
end;function TMyClass.GetYear: Integer;
begin
  Result := YearOf(FDate);
end;procedure TMyClass.SetDay(const Value: Integer);
begin
  FDate :=  RecodeDay(FDate, Value);
end;procedure TMyClass.SetMonth(const Value: Integer);
begin
  FDate := RecodeMonth(FDate, Value);
end;procedure TMyClass.SetValue(d, m, y: Integer);
begin
  FDate := EncodeDate(y, m, d);
end;procedure TMyClass.SetYear(const Value: Integer);
begin
  FDate := RecodeYear(FDate, Value);
end;
end.
Unit1unit Unit1;
interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Button1Read: TButton;
    Button2Write: TButton;
    Button1Write: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button2WriteClick(Sender: TObject);
    procedure Button1ReadClick(Sender: TObject);
    procedure Button1WriteClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation
{$R *.dfm}
uses
  Unit2;var
  Date : TMyClass;procedure TForm1.Button1ReadClick(Sender: TObject);
begin
  Edit1.Text := IntToStr(Date.OnDay);
  Edit2.Text := IntToStr(Date.OnMonth);
  Edit3.Text := IntToStr(Date.OnYear);
end;procedure TForm1.Button1WriteClick(Sender: TObject);
begin
  Date.SetValue(StrToInt(Edit1.Text), StrToInt(Edit2.Text),
  StrToInt(Edit3.Text));
end;procedure TForm1.Button2WriteClick(Sender: TObject);
begin
  Date.OnDay := StrToInt(Edit1.Text);
  Date.OnMonth := StrToInt(Edit2.Text);
  Date.OnYear := StrToInt(Edit3.Text);
end;procedure TForm1.FormCreate(Sender: TObject);
begin
  Date := TMyClass.Create;
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
  Date.Free;
end;
end.关于Button1WriteClick 和 Button2WriteClick 有什么分别?
它们的功能一样吗? 有负面效果吗?
我试来试去, 为什么效果一样的?? (当 Button1ReadClick 显示/发挥效果时.)

解决方案 »

  1.   

    那么EncodeDate 是多余的咯.. >.<;;
      

  2.   

    你看看EncodeDate 的实现代码就知道了
      

  3.   

    看了很多篇, 不明白 >.<;;
      

  4.   

    一樣,無負面影響procedure   TForm1.Button1Click(Sender:   TObject); var 
        Present:   TDateTime; 
        Year,   Month,   Day,   Hour,   Min,   Sec,   MSec:   Word; 
      begin 
        Present:=   Now; 
        DecodeDate(Present,   Year,   Month,   Day); 
        Label1.Caption   :=   'Today   is   Day   '   +   IntToStr(Day)   +   '   of   Month   ' 
            +   IntToStr(Month)   +   '   of   Year   '   +   IntToStr(Year); 
        DecodeTime(Present,   Hour,   Min,   Sec,   MSec); 
        Label2.Caption   :=   'The   time   is   Minute   '   +   IntToStr(Min)   +   '   of   Hour   ' 
            +   IntToStr(Hour); 
    end; 
    procedure   TForm1.Button1Click(Sender:   TObject); var 
        MyDate:   TDateTime; 
    begin 
        MyDate   :=   EncodeDate(StrToInt(Edit1.Text),   StrToInt(Edit2.Text),   StrToInt(Edit3.Text)); 
        Label1.Caption   :=   DateToStr(MyDate); 
    end;
      

  5.   

    Button1WriteClick 和 Button2WriteClick 其中一个去掉可以吗??
    如果去掉 Button1WriteClick, 就意味要去掉 SetValue 咯.. 是吗??
      

  6.   

    你应该看到你那个TMyClass类的本质是对一个TDataTime类型数据的一种包装,其中有设置其值的方法 SetValue以及分别设置其 年 月 日 的属性(虽然属性的命名很别扭,只有事件命名才用On开头),但是,他还是实实在在的分别改变其 年 月 日 的 属性。这个类的定义没有问题,问题是你使用的方式。
         property OnDay : Integer read GetDay write SetDay;
        property OnMonth : Integer read GetMonth write SetMonth;
        property OnYear : Integer read GetYear write SetYear;
    这三个属性,是用来获得或者改变其 年 月 日中某个单一的项(比如单独获得或改变日 调用 onDay,单独获得或改变年调用OnYear等),如果你要三个都改变,那调用SetValue方法 (虽然这个方法的命名也有点别扭,但是在语法上是合法的)。我想,这么说你应该清楚了。仅仅是你使用上的问题。