《Mastering Delphi 6》学习笔记之四

    技术2022-05-11  85

    《Mastering Delphi 6》 学习笔记之四

    C++中,我们可以用static来声明一个属于类而不是类的某个具体实例的函数。许多人以为Object Pascal没有类似的功能,其实是有的,只要将函数声明为类方法(class procedure或者class function)就可以了。例如:

    TForm1=class(TForm)

    public

      class procedure T;

    end;

     

    implementation

    class procedure TFomr1.T;

    begin

    end;

     

    你可以在项目文件中验证它是不是真的static:

    begin

      TForm1.T;

      Application.Initialzie;

      Application.CreateForm(TForm1,Form1);

      Application.Run;

    end;

     

    class procedure/class function在VCL中也是相当重要的一类特殊方法。TObject的ClassName,ClassNameIs,ClassPoint,ClassInfo这四个方法全都是class function。虽然你在实际的编程中99.99999999999%的时间都不会用到它们,不过这四个函数堪称构筑整个VCL的基石。

     

    值得一提的是,与C++中的static member function不同,Object Pascal中的class method能够具有多态的性质。例如,ClassName是TObject的一个class method,但是你调用TForm.ClassName得到的就是'TForm',调用TButton.ClassName得到的就是'TButton',等等。静态方法能够做到多态,岂不是非常神奇?这一切都是通过RTTI才做到的。你可以想象,如果ClassName用一般的virtual method来声明的话,就必须在每个派生类中都必须重载它才能实现类名称的正确映射,那将是多么痛苦的一件事情。

     

     

    Class Reference是Object Pascal中不太为人所知晓的概念。它的语法很简单,比如,

    TClass=class of TObject;

    Class Reference有什么用呢?用一个小例子来说明。在Form上面放一个RadioGroup,其中包括Radio Button, Check Box, Button三项。代码如下:

    type

    TForm1=class(TForm)

      ...

    private

      ControlRef:TControlClass;

      Counter : integer;

    ...

    end;

     

    implementation

    procedure TForm1.FormCreate(Sender:TObject);

    begin

      Counter := 0;

    end;

     

    procedure TForm1.RadioGroup1Click(Sender:TObject);

    begin

      case RadioGroup1.ItemIndex of

    0: ControlRef:=TRadioButton;

    1: ControlRef:=TCheckBox;

    2: ControlRef:=TButton;

      end;

    end;

     

    procedure TForm1.FormMouseDown(Sender:TObject;Button:TMouseButton;Shift:TShiftState;X,Y:integer);

    var

      Control : TControl;

      ControlName : string;

    begin

      if Button=mbLeft then begin

    Control := TControlRef.Create(Self);

    with Control do begin

      Visible := False;

      Parent := Self;

      Left := X;

      Top := Y;

      ControlName := Control.ClassName + IntToStr(Counter);

      Delete(ControlName,1,1);

      Name := ControlName;

      visible := True;

    end;

      end;

    end;

     

    Class Reference揭示了Object Pascal的一个令人惊讶的能力。在OO语言的一般概念中,Constructor不可能是virtual,我们调用TButton.Create建立的就是Button,TForm.Create建立的就是TForm,依此类推。但是,籍由Class Reference,VCL就拥有了virtual Construction的能力,在上面的代码中,通过单独的一个ControlRef.Create,我们建立起来的可以是任何TControl的派生对象。正是由于这种能力,Form Designer和Object Inspector才有办法操作各种各样不同类型的构件,而不管这些构件是Delphi内置的,还是我们从其他地方得到的,或者是我们自己编写的。Class Reference的概念在其他地方也被称作meta-class,是用来描述类信息的特殊类型。实际上,真正的幕后英雄是RTTI,通过RTTI,不仅使得类拥有了virtual construction的功能,而且类中的class method也有了virtual的性质。例如,TObject中的ClassName方法声明为class function ClassName:string,按道理说static method是不可能做到多态的,但实际上,我们调用TButton.ClassName得到的就是TButton,调用TForm.ClassName得到的就是TForm,通过RTTI,不可能的事情变成了可能,而且而避免了在每个派生类都要重载ClassName的麻烦。

    MFC中,如果一个对象满足下列条件:1。它是从CObject派生的;2。它的声明中包含DECLARE_DYNAMIC,并且在某个地方实现了IMPLEMENT_DYNAMIC,那么它也可以通过GetRuntimeClass()->CreateObject来实现类似的功能。所不同的是,MFC的实现方法是通过宏来在内存中建立起一个完整的CRuntimeClass表格,而Object Pascal中,由于单根继承的特性,所有对象天生就具备了这些功能。MFC的内存表格是通过程序员的声明而建立的,而Object Pascal中的RTTI表格是编译器自动生成的,彼此实现方法不同,但背后的思想是基本一致的。

    通过RTTI,不仅对象可以动态生成,而且对象的属性也可以动态设置。下面这段代码不是来自《Mastering Delphi 6》,而是来自陈宽达先生的《Delphi深度历险》,它同样揭示了Object Pascal通过RTTI而具备的强大功能和灵活性:

    var

      i: integer;

      propInfo: PPropInfo;

    begin

      for i:=0 to ComponentCount-1 do begin

    PropInfo := GetPropInfo(Components[i].ClassInfo,’Color’);

    if PropInfo<>nil then

       SetOrProp(Components[i], PropInfo, clRed);

      end;

    end;

     


    最新回复(0)