Delphi Win32中的类帮助器和字符串
当前的delphi有什么方法可以实现吗 a) 带有运算符重载(即+,=)的字符串(作为类) b) 类帮助器,以便添加自定义字符串方法 我认为字符串是本机类型,因此没有Delphi Win32中的类帮助器和字符串,delphi,class,string,operator-overloading,helpers,Delphi,Class,String,Operator Overloading,Helpers,当前的delphi有什么方法可以实现吗 a) 带有运算符重载(即+,=)的字符串(作为类) b) 类帮助器,以便添加自定义字符串方法 我认为字符串是本机类型,因此没有 设置类等。编写自定义函数/过程不是更好的解决方案吗 比如说 Function StrToBase64(AString): string; Procedure StrToGridLayout(AString: string; AGrid: TDBGrid); Function ExtractWord(aString): string
设置类等。编写自定义函数/过程不是更好的解决方案吗 比如说
Function StrToBase64(AString): string;
Procedure StrToGridLayout(AString: string; AGrid: TDBGrid);
Function ExtractWord(aString): string;
Function GetStrColumn(aString: string; aCol: integer): string;
如果要将位于同一单元中的这些功能/过程按功能类别进行分组,可以使用以下记录:
TStringConversions = record
class Function StrToBase64(AString): string;
class Procedure StrToGridLayout(AString: string; AGrid: TDBGrid);
end;
TStringParsing = record
class Function ExtractWord(aString): string;
class Function GetStrColumn(aString: string; aCol: integer): string;
end;
然后,您可以以更简洁的方式在代码中调用它们:
myFirstWord := TStringParsing.ExtractWord('Delphi is a very good tool');
编写自定义函数/过程不是更好的解决方案吗 比如说
Function StrToBase64(AString): string;
Procedure StrToGridLayout(AString: string; AGrid: TDBGrid);
Function ExtractWord(aString): string;
Function GetStrColumn(aString: string; aCol: integer): string;
如果要将位于同一单元中的这些功能/过程按功能类别进行分组,可以使用以下记录:
TStringConversions = record
class Function StrToBase64(AString): string;
class Procedure StrToGridLayout(AString: string; AGrid: TDBGrid);
end;
TStringParsing = record
class Function ExtractWord(aString): string;
class Function GetStrColumn(aString: string; aCol: integer): string;
end;
然后,您可以以更简洁的方式在代码中调用它们:
myFirstWord := TStringParsing.ExtractWord('Delphi is a very good tool');
是的,字符串是一种本机类型,添加了一些特殊的编译器魔法 我不知道你想要什么运算符重载and=已用作串联运算符和相等运算符 然而,我自己也想过做类似的事情。它可能与具有隐式转换器和重载add和equals运算符的记录类型一起工作(在Win32 Delphi中,只有记录可以具有运算符重载。这仅在D2006(?2005)中可用。) 我怀疑也会有一些性能上的问题 语法如下所示:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TString = record
private
Value : string;
public
class operator Add(a, b: TString): TString;
class operator Implicit(a: Integer): TString;
class operator Implicit(const s: string): TString;
class operator Implicit(ts: TString): String;
function IndexOf(const SubStr : string) : Integer;
end;
var
Form1: TForm1;
implementation
class operator TString.Add(a, b : TString) : TString;
begin
Result.Value := a.Value + b.Value;
end;
class operator TString.Implicit(a: Integer): TString;
begin
Result.Value := IntToStr(a);
end;
class operator TString.Implicit(ts: TString): String;
begin
Result := ts.Value;
end;
function TString.IndexOf(const SubStr : string) : Integer;
begin
Result := Pos(SubStr, Value);
end;
class operator TString.Implicit(const s: string): TString;
begin
Result.Value := s;
end;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
ts : TString;
begin
ts := '1234';
ShowMessage(ts);
ShowMessage(IntToStr(Ts.IndexOf('2')));
end;
end.
显然,您也可以使用“记录助手”,但我自己从未尝试过。是的,string是一种原生类型,添加了一些特殊的编译器魔法 我不知道你想要什么运算符重载and=已用作串联运算符和相等运算符 然而,我自己也想过做类似的事情。它可能与具有隐式转换器和重载add和equals运算符的记录类型一起工作(在Win32 Delphi中,只有记录可以具有运算符重载。这仅在D2006(?2005)中可用。) 我怀疑也会有一些性能上的问题 语法如下所示:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TString = record
private
Value : string;
public
class operator Add(a, b: TString): TString;
class operator Implicit(a: Integer): TString;
class operator Implicit(const s: string): TString;
class operator Implicit(ts: TString): String;
function IndexOf(const SubStr : string) : Integer;
end;
var
Form1: TForm1;
implementation
class operator TString.Add(a, b : TString) : TString;
begin
Result.Value := a.Value + b.Value;
end;
class operator TString.Implicit(a: Integer): TString;
begin
Result.Value := IntToStr(a);
end;
class operator TString.Implicit(ts: TString): String;
begin
Result := ts.Value;
end;
function TString.IndexOf(const SubStr : string) : Integer;
begin
Result := Pos(SubStr, Value);
end;
class operator TString.Implicit(const s: string): TString;
begin
Result.Value := s;
end;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
ts : TString;
begin
ts := '1234';
ShowMessage(ts);
ShowMessage(IntToStr(Ts.IndexOf('2')));
end;
end.
显然,您也可以使用“记录帮助器”,但我自己从未尝试过。您可以在Delphi(自Delphi 2006起)中仅在记录上使用运算符重载,而不在类上,也不在内置的本机类型(如字符串)上使用 原因是Delphi没有垃圾收集,因此运算符重载仅限于值类型(不在堆上的类型) 您可以在下载我的会话“带记录、方法和运算符重载的可空类型”的重播。 只需搜索会话名称 还有一个问题 它包含了相当多的例子,让您开始学习,包括对Delphi2006编译器中一些问题的描述,这些问题在Delphi2007及更高版本中已经解决
另请参见此问题:您可以在Delphi(自Delphi 2006起)中仅在记录上使用运算符重载,而不在类上使用,也不在内置的本机类型(如字符串)上使用 原因是Delphi没有垃圾收集,因此运算符重载仅限于值类型(不在堆上的类型) 您可以在下载我的会话“带记录、方法和运算符重载的可空类型”的重播。 只需搜索会话名称 还有一个问题 它包含了相当多的例子,让您开始学习,包括对Delphi2006编译器中一些问题的描述,这些问题在Delphi2007及更高版本中已经解决
另请参见此问题:具体是哪个版本的Delphi?具体是哪个版本的Delphi?您也可以创建记录帮助程序。如果通过赋值将记录复制到另一个记录,是否存在字符串引用计数问题?您也可以创建记录帮助程序。如果复制记录,是否存在字符串引用计数问题通过分配到另一个记录?