看delphi的源码,有些语法看不懂.那位大侠能帮忙解释一下!(50分)

  • 主题发起人 主题发起人 153141271
  • 开始时间 开始时间
1

153141271

Unregistered / Unconfirmed
GUEST, unregistred user!
这下面函数是Tstream中seek方法的实现!
但有些地方不明白!还望大家 指点!!!
?????为不明白的!以前好像 也没用过!
function TStream.Seek(Offset: Longint
Origin: Word): Longint;

procedure RaiseException;???????????怎么函数的的这个位置写个过程,是什么意思?
begin
raise EStreamError.CreateResFmt(@sSeekNotImplemented, [Classname]);
end;

type???????????这个地方也不太懂!
TSeek64 = function (const Offset: Int64
Origin: TSeekOrigin): Int64 of object;
var
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. }
Impl := Seek;
ClassTStream := Self.ClassType;
while (ClassTStream <> nil) and (ClassTStream <> TStream) do
ClassTStream := ClassTStream.ClassParent;
if ClassTStream = nil then RaiseException;
Base := TStream(@ClassTStream).Seek;
if TMethod(Impl).Code = TMethod(Base).Code then
RaiseException;
Result := Seek(Int64(Offset), TSeekOrigin(Origin));???????这个好像是互相调用的,这样可以吗????????
end;
 
function TStream.Seek(Offset: Longint
Origin: Word): Longint;

procedure RaiseException //函数内部使用的私有过程,有些像私有变量
begin
raise EStreamError.CreateResFmt(@sSeekNotImplemented, [Classname]);
end;

type //定义类型
TSeek64 = function (const Offset: Int64
Origin: TSeekOrigin): Int64 of object;
var
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. }
Impl := Seek;
ClassTStream := Self.ClassType;
while (ClassTStream <> nil) and (ClassTStream <> TStream) do
ClassTStream := ClassTStream.ClassParent;
if ClassTStream = nil then RaiseException;
Base := TStream(@ClassTStream).Seek;
if TMethod(Impl).Code = TMethod(Base).Code then
RaiseException;
Result := Seek(Int64(Offset), TSeekOrigin(Origin));
end;
 
function TStream.Seek(Offset: Longint
Origin: Word): Longint;

procedure RaiseException;//这是定义一个函数(Seek)内部的私有过程,外部不能访问
begin
raise EStreamError.CreateResFmt(@sSeekNotImplemented, [Classname]);
end;

type //定义一个函数指针类型,其变量可以指向函数(过程)入口地址,用变量访问函数
TSeek64 = function (const Offset: Int64
Origin: TSeekOrigin): Int64 of object;
var
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. }
//这段话是说32bit 和 64bit 的Seek过程互相(偏移)调用,但TStream类对象不能直
//接用这过程,而应由TStream派生类改写Seek过程并由其派生类对象调用。
Impl := Seek
//将函数地址赋值给TSeek64类型变量Impl
ClassTStream := Self.ClassType

while (ClassTStream <> nil) and (ClassTStream <> TStream) do
ClassTStream := ClassTStream.ClassParent
//循环判断ClassTStream的父类
if ClassTStream = nil then RaiseException
//如果为空则调用私有函数
Base := TStream(@ClassTStream).Seek
//函数地址赋值
if TMethod(Impl).Code = TMethod(Base).Code then //类型转换并判断
RaiseException;
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. }
if (Offset < Low(Longint)) or (Offset > High(Longint)) then
raise ERangeError.CreateRes(@SRangeError);
Result := Seek(Longint(Offset), Ord(Origin));
end;
 
function TStream.Seek(Offset: Longint
Origin: Word): Longint;为提前声明。显然下面没有result:=???。


下面才是一单独抛出异常的完整函数吧
procedure RaiseException
begin
raise EStreamError.CreateResFmt(@sSeekNotImplemented, [Classname]);
end;
 
第一个问题,过程和函数是可以嵌套的,处于里面的函数只有主函数可访问
procedure PA();
procedure P_In();
begin
//P_in的实现代码
end;
begin
 //P_In函数只有PA才能调用
p_in;
end;
 
TSeek64 = function (const Offset: Int64
Origin: TSeekOrigin): Int64 of object;
这是定义了一个方法指针类型,使用方法为:
var
i: integer;
PMth: Tseek64;
begin
i:= PMth(101, ASeekOrigin);//SeekOrign是什么具体不清楚,这时只要给它合法实参就行了
end;
后面的 of object 是说,这个方法是属于某对象的,动态配置
如Aobj.OnSeek:= AMth;
举个通俗的例子:Button1.OnClick:= Form1.OnCreate;这样语句是合法的
定义窗体时,常常看到 在窗体中自动生成 procedure Button1Click(Sender: TOBject);这样的代码,也是一样的道理,跃然你没看见Button1.OnClick:= Button1CLick这一句,但这句在编译的时候确实执行了,IDE的功能
 
多谢大家了
 
楼主怎么只是道谢,不散分呢?
 
多人接受答案了。
 
后退
顶部