2
  Data.XX.NewValue := Data.XX.SavedValue;
  Data.XX.OldValue := Data.XX.SavedValue;

我需要多次执行上述操作,其中XX代表类中的值。假装列表中有 3 个项目:Tim、Bob、Steve。有没有办法在不输入上述代码三次的情况下为所有三个人执行上述操作?

(Data是一个包含多个Objects的类,每个类型为TList,其中包含OldValue、NewValue和SavedValue)

4

4 回答 4

4

如果我不得不做这样的事情,我会在 Data 上再放一个 TList,它包含所有对象的列表。将其填入构造函数中,然后当您必须执行此类操作时,使用循环将相同的基本操作应用于列表中的每个项目。

于 2010-04-13T13:43:15.733 回答
0

也许我不明白,但是……这就是面向对象的亮点。您为该类定义一个过程,然后将其应用于您创建的任何实例。

TMyPropValue = class(TObject)
private
  FNewValue: double;   
  FOldValue: double;
  procedure SetValue(AValue: double);
public
  procedure RestoreOldValue;
  propety NewValue: double read FNewValue write SetValue;   // Raed/write property (write using a procedure)
  property OldValue: double read FOldValue;   // Read only property
end;

TMyClass = class(TObject)
private
  FProp1: TMyPropValue;
  FProp2: TMyPropValue;
public
  procedure RestoreValues;
end;

//....

var
  MyObj1: TMyClass;
  MyObj2: TMyclass;

procedure TMyPropValue.SetValue(AValue: double);
begin
  FOldValue := FNewValue;
  FNewValue := AValue;  
end;

// Restore the Old value of this Prop
procedure TMyPropValue.RestoreOldValue;
begin
  FNewValue := FOldValue;  
end;

// Restore ald the Values of the class
procedure TMyClass.RestoreValues;
begin
  FProp1.RestoreOldValue;
  FProp2.RestoreOldValue;
end;
// -----------

// Creating and populating a couple of objects (instances)
procedure XXX;
begin
  MyObj1 := TMyClass.Create;
  MyObj1.Prop1.NewValue := 10.25:
  MyObj1.Prop2.NewValue := 99.10:

  MyObj2 := TMyClass.Create;
  MyObj2.Prop1.NewValue := 75.25:
  MyObj2.Prop2.NewValue := 60.30:
end;

// Changing values, the class internaly will save the OldValue
procedure yyyy;
begin
  MyObj1.Prop1.NewValue := 85.26:
  MyObj1.Prop2.NewValue := 61.20:

  MyObj2.Prop1.NewValue := 99.20:
  MyObj2.Prop2.NewValue := 55.23:
end;

// Using a procedure from the class
procedure zzzz;
begin
  MyObj1.RestoreValues;
  MyObj2.RestoreValues;
end;

希望这有助于丹尼尔

于 2010-04-13T22:28:12.027 回答
0

这篇文章这篇文章来看,我建议如下:

unit MyAssignment;

interface

  type
    TValueKind = ( EconomicGrowth,
                   Inflation,
                   Unemployment,
                   CurrentAccountPosition,
                   AggregateSupply,
                   AggregateDemand,
                   ADGovernmentSpending,
                   ADConsumption,
                   ADInvestment,
                   ADNetExports,
                   OverallTaxation,
                   GovernmentSpending,
                   InterestRates,
                   IncomeTax,
                   Benefits,
                   TrainingEducationSpending );

        TValue = record
          NewValue,
          OldValue,
          SavedValue : Double;

          procedure SetValue( aVal : Double );
          procedure SaveValue();
          procedure RestoreValue();
        end;

        TDataArray = array [TValueKind] of TValue;

        var
          Data : TDataArray;

implementation

    {TValue}

  procedure TValue.SetValue( aVal : Double );
  begin
    OldValue := NewValue;
    NewValue := aVal;
  end;

  procedure TValue.SaveValue;
  begin
    SavedValue := NewValue;
  end;

  procedure TValue.RestoreValue;
  begin
    NewValue := SavedValue;
    OldValue := SavedValue;
  end;


end.

现在您可以编写这种代码:

            //accessing the values :
            // Data[XX] instead of Data.XX

            //examples :
          ShowMessage(FloatToStr(Data[Inflation].SavedValue));
          Data[AgregateSupply].SetValue( 10.0 );
          Data[Benefits].SaveValue;

          //writing loops :
        procedure RestoreValues( var aData : TDataArray ); //the "var" keyword is important here : google "arguments by value" "arguments by reference"
          var
            lKind : TValueKind;
        begin
          for lKind := Low(TValueKind) to High(TValueKind) do
            aData[lKind].RestoreValue;
        end;

        procedure SaveValues( var aData : TDataArray );
          var
            lKind : TValueKind;
        begin
          for lKind := Low(TValueKind) to High(TValueKind) do
            aData[lKind].RestoreValue;
        end;

          //calling these functions :
        SaveValues( Data );
        RestoreValues( Data );

如果您需要对数组进行更复杂的操作,最好将其放入一个类中 - 仅在 TDataArray 类型的 efield 上替换您编写的字段 - 并将操作数据的函数编写为此类的方法。

于 2010-04-14T11:41:55.007 回答
-3

我会在这里小心。我知道使用通用接口和反射的诱惑,或者其他更灵活的自动化,坦率地说,写起来更有趣。避免这种诱惑。根据您的模式列出列表中的每个项目没有任何问题。模式很好,代码将是可读的、易于执行的,并且易于修改任何不符合该模式的单个属性。

避免输入所有内容的低技术方法是使用我们的老朋友 Excel。将所有属性放在 A 列中,然后在 B 列中使用此公式:

=  CONCATENATE("Data.", A1, ".NewValue := Data.", A1, ".SavedValue;", CHAR(10), "Data.", A1, ".OldValue := Data.", A1, ".SavedValue;", CHAR(10))
于 2010-04-13T13:52:48.513 回答