unit Unit3;interface
type
  TMyDate = class
    private
      fDate : TDateTime;
    public
      procedure Assign(Source : TDate);
      function GetText : string;
  end;
implementation
uses
  SysUtils;{ TMyDate }procedure TMyDate.Assign(Source: TDate);
begin
  fDate := Source;
end;function TMyDate.GetText: string;
begin
  Result := DateToStr(fDate);
end;
end.
unit Unit2;interface
type
  TDate = class
    private
    fDate : TDateTime;
    public
    constructor Create;
    procedure Assign(Source : TDate);
    function GetText : string;
  end;
implementation
uses
  SysUtils;{ TDate }procedure TDate.Assign(Source: TDate);
begin
  fDate := Source.fDate;
end;constructor TDate.Create;
begin
  fDate := Date;
end;function TDate.GetText: string;
begin
  Result := DateToStr(fDate);
end;end.1. 为什么 Unit3 的:
procedure TMyDate.Assign(Source: TDate);
begin
  fDate := Source;
end;
和Unit2 有什么分别??2. Unit2 的 自定类TDate 和 原TDate 有什么分别?? 而这个自定类TDate意味着什么功能?3. 当类名订成 TDate时,为什么在下的要这样设定(指Unit2的 Assign):
fDate := Source.fDate;

解决方案 »

  1.   


    unit Unit2;interface
    type
      TDate2 = class
        private
        fDate : TDateTime;
        public
        constructor Create;
        procedure Assign(Source : TDate2);
        function GetText : string;
      end;
    implementation
    uses
      SysUtils;{ TDate }procedure TDate2.Assign(Source: TDate2);
    begin
      fDate := Source.fDate;
    end;constructor TDate2.Create;
    begin
      fDate := Date;
    end;function TDate2.GetText: string;
    begin
      Result := DateToStr(fDate);
    end;end.
      

  2.   

    1. 为什么 Unit3 的:
    procedure TMyDate.Assign(Source: TDate);
    begin
      fDate := Source;
    end;
    和Unit2 有什么分别??2. Unit2 的 自定类TDate 和 原TDate 有什么分别?? 而这个自定类TDate意味着什么功能?
    TDate意味着什么功能看你定义的成什么样的功能。
    系统默认的是日期类型。
    打一个比方说: 手套和衬衫是两种东西,而你一定要把衬衫也说成手套,也没什么不可以。
    这个时候有人向你要手套,你是给手套还是给衬衫?
    dinoalex说的也不是没有道理,如果你定义的东西都和系统的相同,别人是无法区分和使用的,
    因此,在命名规范上,我们惯例是不能与系统关键字重复。3. 当类名订成 TDate时,为什么在下的要这样设定(指Unit2的 Assign):
    fDate := Source.fDate;
    这个也有问题,不在同一个单元是不能访问其私有类型变量的。
    建议你看看TPersistent类定义的Assign功能。
    即使你修改正确,现在这样做没什么不可以。
    但我可以保证你现在这样做,等你的代码达到几万或几十万行的时候,自己就晕了。
    简单的说:你定义的任何东西都不能与系统关键字重复。
      

  3.   

    TDate系統自帶關鍵字為Double類型,自定義的類名或方法不能和系統有所衝突
      

  4.   

    很深也.. 但为什么 Unit2的 Source.fDate;可以这样,而不像 Unit3的只是 Source...