其他分享
首页 > 其他分享> > Delphi 系统[28]关键字和保留字 index、near、far、export、exports、external、name、resident

Delphi 系统[28]关键字和保留字 index、near、far、export、exports、external、name、resident

作者:互联网

Delphi 系统[28]关键字和保留字  index、near、far、export、exports、external、name、resident

1、定义:

2、示例:

{ index:属性序号 }
type
  TMyObject = class(TObject)
  private
    FLeft: Integer;
    FTop: Integer;
    FWidth: Integer;
    FHeight: Integer;
    function GetInfo(const Index: Integer): Longint;
    procedure SetInfo(const Index: Integer; const Value: Longint);
  public
    property iLeft: Longint index 0 read GetInfo write SetInfo;
    property iTop: Longint index 1 read GetInfo write SetInfo;
    property iWidth: Longint index 2 read GetInfo write SetInfo;
    property iHeight: Longint index 3 read GetInfo write SetInfo;
  end;

function TMyObject.GetInfo(const Index: Integer): Longint;
begin
  case Index of
    0: Result := FLeft;
    1: Result := FTop;
    2: Result := FWidth;
    3: Result :=FHeight;
  end;
end;

procedure TMyObject.SetInfo(const Index: Integer; const Value: Longint);
begin
  case Index of
    0: FLeft := Value;
    1: FTop := Value;
    2: FWidth := Value;
    3: FHeight :=Value;
  end;
end;

----------------------------------------------------------------------------------------
{ index:属性的多个元素 }
type
  TMyObject = class(TObject)
  private
    FList: TStringList;
    function GetItem(Index: Integer): string;
    procedure SetItem(Index: Integer; const Value: string);
  public
    constructor Create;
    destructor Destroy; override;
    property Items[Index: Integer]: string read GetItem write SetItem;
  end;

constructor TMyObject.Create;
begin
  inherited;
  FList := TStringList.Create;
  FList.Add('星期一');
  FList.Add('星期二');
  FList.Add('星期三');
  FList.Add('星期四');
  FList.Add('星期五');
  FList.Add('星期六');
  FList.Add('星期日');
end;

destructor TMyObject.Destroy;
begin
  FList.Free;
  inherited;
end;

function TMyObject.GetItem(Index: Integer): string;
begin
  if (Index >= 0) and (Index <= (FList.Count - 1)) then
    Result := FList[Index]
  else
    Result := 'Out of Index';
end;

procedure TMyObject.SetItem(Index: Integer; const Value: string);
begin
  if (Index >= 0) and (Index <= (FList.Count - 1)) then
    FList[Index] := Value;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  I: Integer;
  MyObj: TMyObject;
begin
  MyObj := TMyObject.Create;
  try
    Caption := MyObj.Items[2];
    MyObj.Items[2] := 'Wednesday';
    for I := 0 to 6 do
      ShowMessage(MyObj.Items[I]);
  finally
    MyObj.Free;
  end;
end;

---------------------------------------------------------------------------------------
{ near }
function Add(A, B: Integer): Integer; near;
{ 如果这个程序被编译为 Test.exe,并且另一个处于本地的程序需要调用这个函数,可以使用以下语句 }
function Add(A, B: Integer): Integer; stdcall; external 'Test.exe';

----------------------------------------------------------------------------------------
{ far }
function Add(a,b: Integer): Integer; far;
{ 如果这个程序被编译为 Test.exe, 并且另一个处于其他计算机的程序需要调用这个函数, 可以使用以下语句 }
function Add(a,b: Integer): Integer; stdcall; external 'Test.exe';

----------------------------------------------------------------------------------------
{ export }
function Add(a,b: Integer): Integer; export;
{ 如果这个程序被编译为 Test.exe, 而另一个程序需要调用这个函数,可以使用以下语句 }
function Add(a,b: Integer): Integer; stdcall; external 'Test.exe';

----------------------------------------------------------------------------------------
{ exports }
library Test;

function TestFunc(I: Integer): string; stdcall;
begin
  Result := IntToStr(I);
end;

exports TestFunc;

begin

end.

{ name 如果输出的对象被重载,则必须给对象起个别名,并注明参数 }
library Test;

function TestFunc(I: Integer): string; overload; stdcall;
begin
  Result := IntToStr(I); 
end;

function TestFunc(S: string): Integer; overload; stdcall;
begin
  Result := StrToInt(S);
end;

exports
  TestFunc(I: Integer) name 'TestFunc1',
  TestFunc(S: string)  name 'TestFunc2';

begin

end.

----------------------------------------------------------------------------------------
{ external }

{$L Test.OBJ}
procedure TestFunc(I:Integer); external;

{ 如果是从 dll 或外部程序中引用,则可以使用以下代码 }
function TestFunc(FileName: string): string; external 'Test.dll';

{ 如果被引用的函数被重载,则必须另外指出引用的名称 }
function MyFunc1(Code: Integer): string; overload; stdcall; external 'Test.dll' name 'TestFunc1';
function MyFunc2(Name: string): Integer; overload; stdcall; external 'Test.dll' name 'TestFunc2';

----------------------------------------------------------------------------------------
{ name }
function MessageBox(HWnd: Integer; Text, Caption: PChar; Flags: Integer): Integer; stdcall; external 'user32.dll' name 'MessageBoxA';

----------------------------------------------------------------------------------------
{ resident }
function Test: string;exports Test name 'MyTest' resident;   //编译时会给出警告:Symbol 'RESIDENT' is deprecated

  

 

 

 

 

 

 

创建时间:2021.08.16  更新时间:

标签:index,exports,end,Index,FList,Longint,Integer,保留字
来源: https://www.cnblogs.com/guorongtao/p/15148249.html