⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unttestclass.pas

📁 在delphi下实现类似于java, C#等反射调用的一个例子
💻 PAS
字号:
unit untTestClass;

interface

uses
  Classes, SysUtils, StrUtils, Dialogs, StdCtrls, Forms;

type
  // Note: {$M+} 和 {$MethodInfo ON} 的顺序不能写反
  // RTTI的优先级高于反射,因此将反射写在里面一层
  
  {$M+}
  {$MethodInfo ON}

  TTestClass = class(TPersistent)
  private
    FText: string;
    FX: integer;
    FOnTest: TNotifyEvent;
    FInnerSelf: TTestClass;
  public
    // 不带参数的
    procedure ShowText;
    function GetText: string;
    // 一个参数
    procedure ShowText2(AText: string);
    function Reverse(AStr: string): string;
    // 二个参数
    function AddInt(X,Y: Integer): Integer;
    // 三个参数
    function Add3(X,Y,Z: Integer): Integer;
    // 对象传入
    procedure ShowCaption(AButton: TButton);
    // 对象传出
    function FindButton(AForm: TForm; AName: string): TButton;
  published
    property Text: string read FText write FText;
    property X: integer read FX write FX;
    property OnTest: TNotifyEvent read FOnTest write FOnTest;
    property InnerSelf: TTestClass read FInnerSelf write FInnerSelf;
  end;

  {$MethodInfo OFF}
  {$M-}

implementation

{ TTestClass }

function TTestClass.Add3(X, Y, Z: Integer): Integer;
begin
  Result := X+Y+Z;
end;

function TTestClass.AddInt(X, Y: Integer): Integer;
begin
  Result := X+Y;
end;

function TTestClass.FindButton(AForm: TForm; AName: string): TButton;
begin
  if AForm.FindComponent(AName) is TButton then
    Result := TButton(AForm.FindComponent(AName))
  else
    Result := nil;
end;

function TTestClass.GetText: string;
begin
  Result := FText;
end;

function TTestClass.Reverse(AStr: string): string;
begin
  Result := StrUtils.ReverseString(AStr);
end;

procedure TTestClass.ShowCaption(AButton: TButton);
begin
  ShowMessage(AButton.Caption);
end;

procedure TTestClass.ShowText;
begin
 ShowMessage(FText);
end;

procedure TTestClass.ShowText2(AText: string);
begin
  ShowMessage(AText);
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -