在Delphi中,我可以从具有相同名称的类方法调用实例方法吗?

时间:2022-11-25 07:55:19

Is it possible in Delphi to have a class method invoke an inherited instance method with the same name? For example, I tried something like this:

在Delphi中有可能让一个类方法调用一个具有相同名称的继承实例方法吗?例如,我尝试过这样的事情:

//... Skipped surrounding class definitions

function TSomeAbstractDialogForm.Execute: Boolean;
begin
  Result := ShowModal = mrOk;
end;

I had a couple of specialized dialog classes that inherited the abstract dialog form, and each class had its own factory method:

我有一些继承抽象对话框表单的专门对话框类,每个类都有自己的工厂方法:

class function TSomeInheritingDialogForm.Execute: Boolean;
var
  Fm: TSomeInheritingDialogForm;
begin
  Fm := TSomeInheritingDialogForm.Create(nil);
  try
    Result := Fm.Execute;
  finally
    Fm.Free;
  end
end;

This approach resulted in a never ending loop since F.Execute, instead of invoking the intended instance method of the base class, kept calling the factory method over and over again (resulting in a pile of created forms).

这种方法导致了一个永无止境的循环,因为F.Execute不是调用基类的预期实例方法,而是一遍又一遍地调用工厂方法(导致一堆创建的表单)。

Of course, the obvious solution was to change the name of the factory method (I named it CreateAndShow), but it made me curious. How come the compiler didn't warn me about the hidden method? And is there a way to explicitly invoke the instance method in a situation like this?

当然,显而易见的解决方案是更改工厂方法的名称(我将其命名为CreateAndShow),但这让我很好奇。为什么编译器没有警告我隐藏的方法?有没有办法在这样的情况下显式调用实例方法?

3 个解决方案

#1


You can try a hard cast. But it is better to rename the class function. (For example to CreateAndExecute).

你可以尝试一个强硬的演员。但最好重命名类函数。 (例如CreateAndExecute)。

The Execute in the child class hides the execute in the parent class (I think the compiler will give a warning for that). You can access this with a hard cast. But there is no way to distinguish between an instance method and a class method.

子类中的Execute隐藏了父类中的execute(我认为编译器会给出一个警告)。你可以通过硬性演员来访问它。但是没有办法区分实例方法和类方法。

function TSomeAbstractDialogForm.Execute: Boolean;
begin
  Result := ShowModal = mrOk;
end;

class function TSomeInheritingDialogForm.Execute: Boolean;
var
  Fm: TSomeInheritingDialogForm;
begin
  Fm := TSomeInheritingDialogForm.Create(nil);
  try
    Result := TSomeAbstractDialogForm(Fm).Execute;
  finally
    Fm.Free;
  end
end;

#2


Result := inherited Execute will not do since it is called on created variable, and not in class method.

结果:=继承执行不会执行,因为它是在创建变量上调用的,而不是在类方法中调用。

The problem is that it's a bad idea to have class function and static method with same name. Compiler is treating them as two separate worlds, that can be written near to each other.

问题是拥有具有相同名称的类函数和静态方法是一个坏主意。编译器将它们视为两个独立的世界,可以彼此靠近地编写。

#3


Wouldn't

Result := inherited Execute;

do the trick?

这个伎俩?

#1


You can try a hard cast. But it is better to rename the class function. (For example to CreateAndExecute).

你可以尝试一个强硬的演员。但最好重命名类函数。 (例如CreateAndExecute)。

The Execute in the child class hides the execute in the parent class (I think the compiler will give a warning for that). You can access this with a hard cast. But there is no way to distinguish between an instance method and a class method.

子类中的Execute隐藏了父类中的execute(我认为编译器会给出一个警告)。你可以通过硬性演员来访问它。但是没有办法区分实例方法和类方法。

function TSomeAbstractDialogForm.Execute: Boolean;
begin
  Result := ShowModal = mrOk;
end;

class function TSomeInheritingDialogForm.Execute: Boolean;
var
  Fm: TSomeInheritingDialogForm;
begin
  Fm := TSomeInheritingDialogForm.Create(nil);
  try
    Result := TSomeAbstractDialogForm(Fm).Execute;
  finally
    Fm.Free;
  end
end;

#2


Result := inherited Execute will not do since it is called on created variable, and not in class method.

结果:=继承执行不会执行,因为它是在创建变量上调用的,而不是在类方法中调用。

The problem is that it's a bad idea to have class function and static method with same name. Compiler is treating them as two separate worlds, that can be written near to each other.

问题是拥有具有相同名称的类函数和静态方法是一个坏主意。编译器将它们视为两个独立的世界,可以彼此靠近地编写。

#3


Wouldn't

Result := inherited Execute;

do the trick?

这个伎俩?