Delphi记录助手为double

Alb*_*ola 4 delphi

情况

我写了一个类似于二级方程的类.你可以在这里找到该课程的完整代码,但它与问题无关.

type
 TArrayOfDouble = array of array of double;

type
 TEqSecGrado = class
  private
   //variables
   a, b, c: double;
   delta: double;
   solutions: TArrayOfDouble;
   solRealCount: integer;
   solImaginaryCount: integer;
   class var currentIstances: integer;
   class var totalIstances: integer;
   //methods
   function getDelta(const vala, valb, valc: double): double; overload;
  public
   constructor Create(const a, b, c: double);
   destructor Destroy; override;
   //methods
   function getDelta: double; overload;
   function getSolutions: TArrayOfDouble; virtual;
   //properties
   property valueOfA: double read a;
   property valueOfB: double read b;
   property valueOfC: double read c;
   property realSolutionsCount: integer read solRealCount;
   property imaginarySolutionsCount: integer read solImaginaryCount;
   class property currentEquationsCount: integer read currentIstances;
   class property totalEquationsCount: integer read totalIstances;
 end;
Run Code Online (Sandbox Code Playgroud)

我想创建一个名为的新类型TArrayOfDouble,它将包含我方程的解.在主窗体中,我以这种方式使用类:

procedure TForm3.Button1Click(Sender: TObject);
var solver: TEqSecGrado;
    soluzioni: TArrayOfDouble;
begin

 //f(x) = 3x^2 - x - 2 -> [sol.1 = 1 | sol.2 = -0,666666666666667]
 solver := TEqSecGrado.Create(3,-1,-2);

 try
  soluzioni := solver.getSolutions; 
  //soluzioni[0][0] = 1; soluzioni[0][1] = 0; 
  //soluzioni[1][0] = -0,666666666666667; soluzioni[1][1] = 0; 
 finally
  solver.Free;
 end;

end;
Run Code Online (Sandbox Code Playgroud)

现在我在里面有结果soluzioni,我可以输出它们.(我使用了一个矩阵,因为在第一个地方我放了真正的解决方案,在这种情况下是1和-0.67,如果需要的话,在第二个运动中可以使用虚拟解决方案).


当我输出解决方案时,我想将它们转换为分数.我想做点什么soluzioni[a][b].toFraction.所以我以为我可以使用记录助手来实现双倍.

type
 TSupport = record helper for Double
   function toFraction: string;
   function toString: string; //I have added this LATER
 end;
Run Code Online (Sandbox Code Playgroud)

在这里,我怀疑.一旦我创建了这个TSupporttoFraction方法,我就能调用soluzioni [0] [0] .toFraction,但我无法调用soluzioni [i] [0] .toString.

为了解决我的问题,我决定添加函数toString,一切正常.记录助手是否隐藏了所有其他方法?如果我删除了记录助手,我可以像往常一样再次使用toString方法.

我知道帮助器是一种在不使用继承的情况下扩展类的方法,但为什么我只能使用我在助手中声明的方法?

Dav*_*nan 7

类和记录助手的限制是一次只能激活一个.该ToString方法Double实际上是在Delphi RTL提供的记录助手中实现的.将助手附加到类型后,RTL助手不再处于活动状态.

文件说:

您可以使用单个类型定义和关联多个帮助程序.但是,源代码中的任何特定位置只应用零或一个助手.最近范围中定义的帮助程序将适用.类或记录助手范围以正常的Delphi方式确定(例如,在单元的uses子句中从右到左).

这是一个长期存在的问题,自从助手被引入以来,Embarcadero就已经知道了这个问题.在很长一段时间里,他们没有解决这个问题,而且我认为你应该假设他们永远不会这样做.

所以,你有两个选择:

  1. 附上您自己的帮助程序,并接受您无法使用RTL帮助程序的功能.
  2. 通过助手以外的方式实现您的功能.

  • 就像一个FYI:您可以同时激活多个CLASS助手.CLASS助手支持继承,因此你可以实际定义你的类助手作为前一个的扩展,如_TStringsHelper = CLASS HELPER(VclHelpers.TStringsHelper)FOR TStrings_和VclHelpers.TStringsHelper以及你刚刚从当前UNIT声明的TStringsHelper将可以访问.遗憾的是,RECORD助手不支持继承,因此我发现"扩展"现有RECORD助手的唯一方法是将助手重新实现为调用旧助手的INLINE方法. (3认同)