情况
我写了一个类似于二级方程的类.你可以在这里找到该课程的完整代码,但它与问题无关.
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)
在这里,我怀疑.一旦我创建了这个TSupport和toFraction方法,我就能调用soluzioni [0] [0] .toFraction,但我无法调用soluzioni [i] [0] .toString.
为了解决我的问题,我决定添加函数toString,一切正常.记录助手是否隐藏了所有其他方法?如果我删除了记录助手,我可以像往常一样再次使用toString方法.
我知道帮助器是一种在不使用继承的情况下扩展类的方法,但为什么我只能使用我在助手中声明的方法?
类和记录助手的限制是一次只能激活一个.该ToString方法Double实际上是在Delphi RTL提供的记录助手中实现的.将助手附加到类型后,RTL助手不再处于活动状态.
该文件说:
您可以使用单个类型定义和关联多个帮助程序.但是,源代码中的任何特定位置只应用零或一个助手.最近范围中定义的帮助程序将适用.类或记录助手范围以正常的Delphi方式确定(例如,在单元的uses子句中从右到左).
这是一个长期存在的问题,自从助手被引入以来,Embarcadero就已经知道了这个问题.在很长一段时间里,他们没有解决这个问题,而且我认为你应该假设他们永远不会这样做.
所以,你有两个选择: