【问题标题】:Structure Delphi Generics Class结构 Delphi 泛型类
【发布时间】:2011-01-11 00:17:14
【问题描述】:

我是泛型新手,需要一些帮助来构建类和实现方法。

我正在尝试使用泛型序列化 any TObject-JSON。此外,我希望能够重用代码。

这些是我的问题:

  1. 如何创建泛型构造函数?我希望能够使用SelfDefault(T),但它只返回nil

  2. V := Marshal.Marshal(ReturnObject) - 这个方法需要TObject,但是我不知道如何引用当前传入的对象。

  3. 如何在方法中使用它?看看下面截取的代码,标有“问题 3”。

这是我的代码:

TFileOperationResult = class(TObject)
private
  FSuccess: Boolean;
  //Error: PException;
  FLastError: Integer;
  function GetFailure: Boolean;
  property Failure: Boolean read GetFailure;
public
  property Success: Boolean read FSuccess write FSuccess;
  property LastError: Integer read FLastError write FLastError;
end;

TResponseObject<T: class> = class(TObject)
private
  FReturnObject: T;
  function GetReturnObject: T;
  function BaseStringsConverter(Data: TObject): TListOfStrings;
public
  constructor Create; overload;
  property ReturnObject: T read GetReturnObject;
  procedure Serialize;
end;

constructor TResponseObject<T>.Create;
begin
 // Question 1 - What should go in here?
end;

function TResponseObject<T>.GetReturnObject: T;
begin
  Result := Default(T);// Is this correct?
end;

procedure TResponseObject<T>.Serialize;
var
  Marshal: TJSONMarshal;
  V: TJSONValue;
begin
  Marshal := TJSONMarshal.Create(TJSONConverter.Create);
  Marshal.RegisterConverter(TResponseObject<T>, BaseStringsConverter);
  V := Marshal.Marshal(ReturnObject); // Question 2 - How Can I refer to 'Self'?
  OutPut := V.ToString;
  Marshal.Free;
end;

调用代码:

procedure TForm1.Test;
var
  FileOperationResult: TResponseObject<TFileOperationResult>;
begin
  FileOperationResult := TResponseObject<TFileOperationResult>.Create;
  FileOperationResult.Serialize;
end;

问题 3:

procedure TForm1.MoveCopyFile<THowNowResponse>(ASource, DDestination: String);
var
  FileOperationResult: TFileOperationResult;
begin
  FileOperationResult := TFileOperationResult.Create;
  // What to do?
end;

非常感谢任何其他 cmets。

【问题讨论】:

  • 真的很难知道你想要什么......我认为最好单独写一个问题,因为你似乎有不同的问题,并试图在这里解决所有问题。对于泛型部分... JSON Marshaler 能够编组任何类,所以我不确定您是否要创建一个泛型类以将其用作包装器来编组任何对象,只需使用默认 TJsonConverter 或什么(也许TResponseObject 类名让我不清楚)。

标签: delphi generics delphi-2010


【解决方案1】:

很难准确地说出您在这里要做什么,但我可以猜测一下。对于 TResponseObject,您需要一个可以包含另一个对象并对其进行操作的对象。在这种情况下,您可能希望将其传递给构造函数,如下所示:

constructor TResponseObject<T>.Create(value: T);
begin
  FReturnObject := value;
end;

同样,如果您创建一个 GetReturnObject 方法,它可能应该返回 FReturnObject 字段的值。 (或者您可以让属性的 read 访问器直接引用 FReturnObject。)

function TResponseObject<T>.GetReturnObject: T;
begin
  Result := FReturnObject;
end;

回答 #3 真的很难,因为我不知道您要对此做什么,但希望我对前两个的回答能帮助您重回正轨。请记住,泛型不必令人困惑;它们基本上只是类型替换。在非泛型例程中使用普通类型的任何地方,都可以将其替换为 &lt;T&gt; 以创建泛型例程,然后替换任何适合特定 T 约束的类型。

【讨论】:

  • 您好 Mason,可以通过 MSN 或 Google Talk 与您联系吗?
  • @Phillip:如果您还有其他问题,我希望您在此处发布。这样,将来遇到类似问题的其他人也可以从答案中受益。
  • 感谢 Mason,不使用泛型,我有以下代码将对象转换为 JSON:procedure Serialize; var 元帅:TJSONMarshal;文件结果:TFileOperationResult;五:TJSON值;开始 FileResult := TFileOperationResult.Create; FileResult.Success := 真; FileResult.LastError := 100;元帅 := TJSONMarshal.Create(TJSONConverter.Create); //Marshal.RegisterConverter(TFileOperationResult, BaseStringsConverter); V := Marshal.Marshal(FileResult);输出:= V.ToString; FileResult.Free;元帅免费;结束;
  • 输出如下: {"type":"uJSonSerializer.TFileOperationResult","id":1,"fields":{"FSuccess":true,"FLastError":100}} 在简而言之,我要做的是传入任何对象,并使用泛型将其转换为 JSON。
  • 使用泛型时,我得到以下不正确的结果。 (我像以前一样寻找结果)。 {"type":"DBXJSONReflect.TJSONConverter","id":1,"fields":{"FRoot":{"type":"DBXJSON.TJSONObject","id":2,"fields":{"FMembers ":{"type":"DBXPlatform.TDBXArrayList","id":3,"fields":{"FList":{"type":"Contnrs.TObjectList","id":4,"fields":{ "FOwnsObjects":false,"FCount":3,"FCapacity":4}}}},"FOwned":true}},"FStack":{"type":"Generics.Collections.TStack ","id":5,"fields":{"FCount":8,"FItems": ... etc etc 非常感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-19
  • 1970-01-01
  • 1970-01-01
  • 2016-09-14
  • 2013-03-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多