无论如何有使用行为的属性吗? [英] Is there anyway to use the property like behavior?
问题描述
我有以下公式
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;
如果可能的话,我想使用它但不使用类/记录实现(不在类内部) / recordimplementation / method)。不是确切的语法,只是原理相同,而不是直接分配给X,而是调用SetX,然后将结果分配给X。
If it was possible I would like to use this but without class/record implementation(not inside a class/record implementation/method).not the exact syntax, just the same principle, instead of direct assignment to X the SetX is called then the result is assigned to 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循环将如下所示:
So I could omit Line (1). If this was possible, all the lines after the formula would be omitted and the while loop would look like this
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;
是否仍然可以像行为一样使用属性?
注意:我正在寻找一种方法来更改变量的赋值和读取方式,就像在记录/类的属性中所做的一样。
Note: I'm looking for a way to alter the assignment and reading ways of a variable like you do in a property of a record/class.
推荐答案
是否仍然可以在类/记录之外使用诸如方法之类的属性?
Is there anyway to use the property like approach outside a class/record?
不,属性获取器和设置器只能在记录和类中实现。
No, property getters and setters can only be implemented in records and classes.
这篇关于无论如何有使用行为的属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!