Delphi IS运算符-运算符不适用于此操作数类型 [英] Delphi IS operator - Operator not applicable to this operand type

查看:173
本文介绍了Delphi IS运算符-运算符不适用于此操作数类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这应该很容易,因为我一定做错了。

I guess this should be an easy one cause I must be doing something wrong.

这是我的代码,我试图在Delphi中做一个Strategy模式:

this is my code, I'm trying to do a Strategy pattern in Delphi:

unit Pattern;

interface

type

  TContext = class;

  IStrategy = interface
    function Move(c: TContext): integer;
  end;

  TStrategy1 = class(TInterfacedObject, IStrategy)
  public
    function Move(c: TContext): integer;
  end;

  TStrategy2 = class(TInterfacedObject, IStrategy)
  public
    function Move(c: TContext): integer;
  end;

  TContext = class
  const
    START = 5;
  private
    FStrategy: IStrategy;
  public
    FCounter: integer;
    constructor Create;
    function Algorithm(): integer;
    procedure SwitchStrategy();
  end;

implementation

{ TStrategy1 }

function TStrategy1.Move(c: TContext): integer;
begin
  c.FCounter := c.FCounter + 1;
  Result := c.FCounter;
end;

{ TStrategy2 }

function TStrategy2.Move(c: TContext): integer;
begin
  c.FCounter := c.FCounter - 1;
  Result := c.FCounter;
end;

{ TContext }

function TContext.Algorithm: integer;
begin
  Result := FStrategy.Move(Self)
end;

constructor TContext.Create;
begin
  FCounter := 5;
  FStrategy := TStrategy1.Create();
end;

procedure TContext.SwitchStrategy;
begin
  if FStrategy is TStrategy1 then
    FStrategy := TStrategy2.Create()
  else
    FStrategy := TStrategy1.Create();
end;

end.

如果FStrategy是TStrategy1,则会给我:运算符不适用于此操作数类型。
我在做什么错,因为从许多Delphi语言参考中可以理解,这应该可以工作?

And the if FStrategy is TStrategy1 then is giving me: Operator not applicable to this operand type. What am I doing wrong here cause this should work as I understand from a lot of Delphi language references?

推荐答案

您已从界面中省略了GUID。 没有它就无法工作。

You have omitted the GUID from your interface. is can't work without it.

编辑:乍一看,它仍然无法工作。您不能使用 is 在Delphi中测试接口引用的实现对象类型(当然,无论如何,都不是直接的)。您应该更改设计。例如,您可以更改接口或添加另一个接口以返回实现的描述。

On second glance, it still won't work. You can't use is to test an interface reference for its implementing object typein Delphi (well, not directly, anyway). You should change your design. For example, you could either alter the interface or add another interface to return a description of the implementation.

这篇关于Delphi IS运算符-运算符不适用于此操作数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆