Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 从类数组创建实例_Arrays_Delphi_Oop_Polymorphism_Delphi 10 Seattle - Fatal编程技术网

Arrays 从类数组创建实例

Arrays 从类数组创建实例,arrays,delphi,oop,polymorphism,delphi-10-seattle,Arrays,Delphi,Oop,Polymorphism,Delphi 10 Seattle,我的班级定义是: TAnimal = class(TInterfacedObject) public constructor Create; overload; constructor Create(param : string); overload; end; IAnimal = interface procedure DoSomething; end; TDog = class(TAnimal, IAnimal) public procedure DoSo

我的班级定义是:

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;
假设你有一个表单,上面有三个按钮。“狗”按钮、“猫”按钮和“进程队列”按钮。当您按下“Dog”按钮或“Cat”按钮时,相关的类将被添加到数组中作为队列。然后,当您按下“ProcessQueue”按钮时,程序将逐步遍历数组,创建相关类的对象,然后调用在该类中实现的接口方法。记住上面的示例代码,如何实现这一点

简单的方法显然是将类名作为字符串添加到字符串数组中,然后在
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
块。

您可以使用。按如下方式定义类引用类型:

type
  TAnimalClass = class of TAnimal;
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;
并安排
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>; 
您需要在适当的位置创建并销毁
myQueue
的实例。我想你已经知道怎么做了

使用类引用时一个更细微的差别是,通常在基类中提供一个虚拟构造函数。这是因为当使用类引用创建实例时,调用基类中声明的构造函数。如果该构造函数不是虚拟的,则不会执行派生类构造函数代码


当然,以这种方式使用类引用会使接口变得毫无意义

类引用是Delphi/object pascal语言的真正精华。我非常怀念Java或C中的这个特性!另外请注意,使用虚拟构造函数创建是有意义的,否则实现迟早会受到限制。@DavidHeffernan谢谢!我的实际实现要比这复杂一些,但是您的回答对于帮助我正确地布置基础是非常宝贵的。谢谢你详细的回答。@J。。。添加了明确的问题陈述,而不是暗示问题陈述。在本例中,我使用的是Delphi Seattle。