美好的一天,
我编写了一些类TFoo
和TBar
。他们有一些方法需要调用swithcing函数TFoo.switch
和TBar.switch
TBaseFooBar = class
strict private
FNumConfig: Word;
protected
procedure Switch;
end;
这个超级类有一个私有字段FNumConfig
,用于描述PCI卡的数量。 Switch
方法使用某个库中的函数:
procedure TBaseFooBar.Switch;
begin
tmkselect(FNumConfig);
end;
我想问你有没有办法在执行任何Switch
或DoSomethingWithFoo()
之前调用超级函数DoSomethingWithBar()
?
我的意思是这样的
TFoo = class(TBaseFooBar )
{ FNumConfig: Word; // this number declares the number of a PCI card}
public
@Switch // before this method
procedure DoSomethingWithFoo1();
// ...
@Switch // and before this one
procedure DoSomethingWithFooN();
// before this one there is no calling of Switch
function NotSwitching();
end;
TBar
类
TBar = class(TBaseFooBar )
{ FNumConfig: Word; // this is another one number declares of another PCI card, they are not equal. }
public
@Switch // before this method
procedure DoSomethingWithBar1();
// ...
@Switch // and before this one
procedure DoSomethingWithBarN();
end;
我知道一个简单的方法:
procedure TFoo.DoSomethingWithFoo1();
begin
Switch();
// and another stuff.
end;
但我想把它全部写在一个地方 - 在一个类的声明中。
答案 0 :(得分:1)
Delphi不支持内置的任何内容。您要求的内容本质上是面向方面的编程功能。第三方供应商提供了一些选项,have been discussed here before。
答案 1 :(得分:0)
有一些面向对象的设计模式可能有助于解决这个问题:
这意味着:操作可以实现为“Command”对象,在需要时由“Switcher”类装饰。