TStream 使用了下面的代码,强制子类必须implement两个seek方法中至少一个
classes.pas 单元4904行@Delphi7
首先说明TStream有两个seek方法
function Seek(Offset: Longint; Origin: Word): Longint; overload; virtual; //叫它seek1
function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; overload; virtual;//叫它seek2
看看如何实现的
function TStream.Seek(Offset: Longint; Origin: Word): Longint;
procedure RaiseException;
begin
raise EStreamError.CreateResFmt(@sSeekNotImplemented, [Classname]);
end;
type
//声明了一个seek2样式的函数类型
TSeek64 = function (const Offset: Int64; Origin: TSeekOrigin): Int64 of object;
var
//impl是子类的seek2方法,Base是TStream的seek2方法
Impl: TSeek64;
Base: TSeek64;
ClassTStream: TClass;
begin
{ Deflect 32 seek requests to the 64 bit seek, if 64 bit is implemented.
No existing TStream classes should call this method, since it was originally
abstract. Descendent classes MUST implement at least one of either
the 32 bit or the 64 bit version, and must not call the inherited
default implementation. }
//如果进入此函数,说seek1没有被override,下面的任务就是验证seek2必须被override;
Impl := Seek; //如果子类没有实现seek,那么impl就是seek2的方法(庐山中?)
//下面将ClassTStream将会是TStream的class类型,同时保证当前类是TStream的子类
ClassTStream := Self.ClassType;
while (ClassTStream <> nil) and (ClassTStream <> TStream) do
ClassTStream := ClassTStream.ClassParent;
if ClassTStream = nil then RaiseException;
//Base为TStream的seek2方法,这句的语法最有意思
Base := TStream(@ClassTStream).Seek;
//开始了,如果当前类的seek2方法和TStream的seek2方法代码相同,那么异常之
if TMethod(Impl).Code = TMethod(Base).Code then
RaiseException;
//能执行到这里,表明seek2已经被override了,调用子类实现的seek2方法
Result := Seek(Int64(Offset), TSeekOrigin(Origin));
end;
function TStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64;
begin
{ Default implementation of 64 bit seek is to deflect to existing 32 bit seek.
Descendents that override 64 bit seek must not call this default implementation. }
//这里就好解释了,如果执行到这个方法,那么说明本seek2未被override;,那么seek1一定被override了,就调用seek1.
if (Offset < Low(Longint)) or (Offset > High(Longint)) then
raise ERangeError.CreateRes(@SRangeError);
Result := Seek(Longint(Offset), Ord(Origin));
end;
我刚开始看到 时候,还说怎么回事,两个overload的函数互相调用,没有具体代码,原来是要求子类必须实现其中一个,borland的工程师确实很牛!!!