泛型與無聊

雲水浮萍發表於2015-01-16
type
  TActionComponent = class(TObject)
  public
     procedure Execute;virtual;
  end;

  TStuckPig = class(TActionComponent)
  public
     procedure Execute;override;
  end;

  TRole = class(TObject)
  public
    Name:String[255];
    ActionList:TList<TActionComponent>;
  end;

  TWorld = class(TObject)
  public
     ObjectList:TDictionary<string,TList<TRole>>;
  end;

  TForm2 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
var
  World:TWorld;
  Roles:TList<TRole>;
  Role:TRole;
  ActionComponent:TActionComponent;
begin
  World:=TWorld.Create;

  //角色:屠夫
  Role:=TRole.Create;
  Role.Name:='屠夫';
  //殺豬動作或者叫技能可能更準確
  ActionComponent:=TStuckPig.Create;
  Role.ActionList.Add(ActionComponent);
  Roles:=TList<TRole>.Create;
  Roles.Add(Role);

  //角色:將軍
  Role:=TRole.Create;
  Role.Name:='驃騎大將軍';
  Roles.Add(Role);
  //角色:書法家
  Role:=TRole.Create;
  Role.Name:='書法家';
  Roles.Add(Role);
  //角色:酒鬼
  Role:=TRole.Create;
  Role.Name:='酒鬼';
  Roles.Add(Role);
  //賦與張飛各角色
  World.ObjectList.Add('張飛',Roles);

end;

 

相關文章