【发布时间】:2017-10-30 17:43:52
【问题描述】:
我有以下公式
X := X + F*(1-i div n);
在哪里
X, F, i, n: integer;
我使用的代码是这样的
F := 7; // the initial speed with no friction.
n := 10; // the animation number of steps.
Hn := n * 2 ;
X := 0; // first Pos
i := 1;
J := 1;
while J < Hn do
begin
X := X + F * (1 - i div n);
if X > Xmax then X := 0; <-- line (1).
if i >= n then Dec(i)
else Inc(i);
Inc(J);
end;
如果可能的话,我想使用它但没有类/记录实现(不在类/记录实现/方法内)。不是确切的语法,只是相同的原理,而不是直接分配给 X,SetX 是调用然后将结果分配给 X。
X: integer write SetX; // This is not a correct delphi syntax. I added it to explain the behavior I want.
function SetX(aValue: integer): integer;
const
Xmax: SomeIntegerValue;
begin
if aValue > Xmax then result := 0
else result := aValue;
end;
所以我可以省略第 (1) 行。如果可能,公式后面的所有行都将被省略,while 循环将如下所示
while J < Hn do // J will be incremented each time the loop wants to read it.
begin
X := X + F * (1 - i div n);
end;
是否有使用类似行为的属性?
注意:我正在寻找一种方法来改变变量的赋值和读取方式,就像您在记录/类的属性中所做的那样。
【问题讨论】:
-
我会在这里使用
IfThen。 -
如果您的循环也在记录方法中,您仍然可以仅对
X := X + F * (1 - i div n);进行正确处理,我认为这是实现这一点的最佳方式。 -
我不确定你所说的通用是什么意思,你仍然需要把它放在一些程序中。您在代码中的唯一区别是,您将编写
MyRecord.MyProcedure,而不是像MyProcedure那样调用它。其他一切都一样。 -
@EugeneK 我知道这一点(阅读我的第一条评论或第二条注释)。我想我在上一条评论中也解释了 universal。
-
编程到语言中。你逆流而上。不。使用本地函数。无论如何,它会更加明确和清晰。这是一个更好的解决方案。
标签: delphi properties delphi-10-seattle