我的班级定义是: TAnimal = class(TInterfacedObject)public constructor Create; overload; constructor Create(param : string); overload;end;IAnimal = interface procedure DoSomething;end;TDog = class(TAnimal, IAnimal)public procedure Do
TAnimal = class(TInterfacedObject) public constructor Create; overload; constructor Create(param : string); overload; end; IAnimal = interface procedure DoSomething; end; TDog = class(TAnimal, IAnimal) public procedure DoSomething; end; TCat = class(TAnimal, IAnimal) public procedure DoSomething; end;
示例代码:
procedure TForm1.DogButtonPressed(Sender: TObject); var myDog : TDog; I : Integer; begin myDog := TDog.Create('123'); I := Length(myQueue); SetLength(myQueue, I+1); myQueue[I] := TDog; //Probably not the way to do it...?? end; procedure TForm1.CatButtonPressed(Sender: TObject); var myCat : TCat; I : Integer; begin myCat := TCat.Create('123'); I := Length(myQueue); SetLength(myQueue, I+1); myQueue[I] := TCat; //Probably not the way to do it...?? end; procedure TForm1.OnProcessQueueButtonPressed(Sender: TObject); var MyInterface : IAnimal; //Interface variable I : Integer; begin for I := Low(myQueue) to High(myQueue) do begin MyInterface := myQueue[I].Create('123'); //Create instance of relevant class MyInterface.DoSomething; end; end;
所以,假设你有一个带有三个按钮的表单. “狗”按钮,“猫”按钮和“处理队列”按钮.当您按下“狗”按钮或“猫”按钮时,相关的类将添加到数组中以充当队列.然后按“Process Queue”按钮,程序逐步执行数组,创建相关类的对象,然后调用在该类中实现的接口方法.记住我的示例代码,如何实现?
显而易见的方法是将类名作为字符串添加到字符串数组中,然后在OnProcessQueueButtonPressed过程中使用if语句,例如:
procedure TForm1.OnProcessQueueButtonPressed(Sender: TObject); var MyInterface : IAnimal; //Interface variable I : Integer; begin for I := Low(myQueue) to High(myQueue) do begin if myQueue[I] = 'TDog' then MyInterface := TDog.Create('123'); if myQueue[I] = 'TCat' then MyInterface := TCat.Create('123'); MyInterface.DoSomething; end; end;
我试图避免这种情况,因为每次我添加一个新类时,我都要记得为新类添加一个if块.
您可以使用 class reference执行此操作.定义类引用类型,如下所示:type TAnimalClass = class of TAnimal;
并安排TAnimal支持接口:
type IAnimal = interface procedure DoSomething; end; TAnimal = class(TInterfacedObject, IAnimal) public constructor Create; overload; constructor Create(param: string); overload; procedure DoSomething; virtual; abstract; end; TDog = class(TAnimal) public procedure DoSomething; override; end; TCat = class(TAnimal) public procedure DoSomething; override; end;
使用数组会导致代码混乱.更好的是使用列表对象.
var myQueue: TList<TAnimalClass>;
现在您可以编写如下代码:
procedure TForm1.DogButtonPressed(Sender: TObject); begin myQueue.Add(TDog); end; procedure TForm1.CatButtonPressed(Sender: TObject); begin myQueue.Add(TCat); end; procedure TForm1.OnProcessQueueButtonPressed(Sender: TObject); var AnimalClass: TAnimalClass; Animal: IAnimal; begin for AnimalClass in myQueue do begin Animal := AnimalClass.Create('123'); Animal.DoSomething; end; myQueue.Clear; end;
您需要在适当的位置创建和销毁myQueue的实例.我假设你已经知道如何做到这一点.
使用类引用时,一个更好的细微差别是您通常在基类中提供虚拟构造函数.这是因为当您使用类引用来创建实例时,您将调用在基类中声明的构造函数.如果该构造函数不是虚拟的,则不会执行派生类构造函数代码.
当然,以这种方式使用类引用会使界面变得毫无意义.