作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有基础课
TWMapObject = class (TObject)
private
AFname: string;
FFileHandler: TWMapFileHandler;
function TryOpenFileHandler(const fName: string): TWMapFileHandler;
public
property FileHandler: TWMapFileHandler read FFileHandler;
constructor Create(const fName: string);
destructor Destroy; override;
end;
TBlpTexture = class (TWMapObject)
public
width, height: integer;
id: GLuint;
hdr: TBLP2Header;
constructor Create(const fname: string);
destructor Destroy; override;
procedure LoadBlp;
end;
TWMapObjectClass = class of TWMapObject;
TWMapObjectManager<T: TWMapObject, constructor> = class
type
PManagerRec = ^TManagerRec;
TManagerRec = record
obj: T;
ref: integer;
end;
private
ht: TDictionary<string, PManagerRec>;
function CreateNewT(const fName: string): T;
public
constructor Create;
destructor Destroy; override;
procedure Add(const fName: string);
procedure Remove(const fName: string);
procedure Clear;
function Get(const fName: string): T;
end;
TWMapObject
的类用作通用参数
T
,用于该辅助函数
function TWMapObjectManager<T>.CreateNewT(const fName: string): T;
var
obj: TWMapObject;
ct: TWMapObjectClass;
begin
ct := TWMapObjectClass(GetTypeData(TypeInfo(T)).ClassType);
obj := ct.Create(fName);
Move(obj, result, SizeOf(pointer));
end;
procedure TWMapObjectManager<T>.Add(const fName: string);
var
pr: PManagerRec;
begin
if ht.TryGetValue(fName, pr) then
inc(pr.ref)
else begin
GetMem(pr, sizeof(TManagerRec));
pr.obj := CreateNewT(fName);
pr.ref := 1;
ht.Add(fName, pr);
end
end;
TexManager: TWMapObjectManager<TBlpTexture>
。该代码适用于基于
TControl
的对象,例如
TButton
等,并在以前的项目中创建了精确的
TButton
class function TLuaClassTemplate<T>.CreateNewT(AOwner: TComponent): T;
var
Ctl: TControl;
begin
Ctl := TControlClass(GetTypeData(TypeInfo(T)).ClassType).Create(AOwner);
Move(Ctl, result, SizeOf(pointer));
end;
TBlpTexture
视为ct的值,但仅调用
TWMapObject
的构造函数,而不是
TBlpTexture
。难道我做错了什么?可以解决吗?
最佳答案
您需要一个虚拟构造函数:
type
TWMapObject = class(TObject)
....
constructor Create(const fName: string); virtual;
....
end;
type
TWMapObjectClass = class of TWMapObject;
type
TBlpTexture = class(TWMapObject)
public
....
constructor Create(const fName: string); override;
....
end;
constructor
约束(据我所知,它几乎是无用的):
type
TWMapObjectManager<T: TWMapObject> = class
....
end;
CreateNewT
:
function TWMapObjectManager<T>.CreateNewT(const fName: string): T;
begin
Result := T(TWMapObjectClass(T).Create(fName));
end;
{$APPTYPE CONSOLE}
type
TWMapObject = class
constructor Create(const fName: string); virtual;
end;
TWMapObjectClass = class of TWMapObject;
TBlpTexture = class(TWMapObject)
public
constructor Create(const fName: string); override;
end;
TWMapObjectManager<T: TWMapObject> = class
function CreateNewT(const fName: string): T;
end;
{ TWMapObject }
constructor TWMapObject.Create(const fName: string);
begin
Writeln(ClassName);
end;
{ TBlpTexture }
constructor TBlpTexture.Create(const fName: string);
begin
inherited;
Writeln(fName);
end;
{ TWMapObjectManager<T> }
function TWMapObjectManager<T>.CreateNewT(const fName: string): T;
begin
Result := T(TWMapObjectClass(T).Create(fName));
end;
begin
TWMapObjectManager<TBlpTexture>.Create.CreateNewT('Foo');
Readln;
end.
关于delphi - 基于泛型和TObject的T问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18578857/
我是一名优秀的程序员,十分优秀!