如何在VCL类中使用接口? [英] How to use Interface with VCL Classes?

查看:73
本文介绍了如何在VCL类中使用接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在上一个问题中,建议我使用接口:
如何使用2个或更多的类来实现相同的方法?

In my previous question I was suggested to use Interface: How to implement identical methods with 2 and more Classes?

但是由于我对如何使用接口一无所知,因此没有任何合规性。

But nothing complies as I have no knowledge on how to use Interfaces.

我想您可以将此代码称为伪:)

I guess you can call this code pseudo :)

type
  IDoSomething = interface
    ['{EFE0308B-A85D-4DF3-889C-40FBC8FE84D0}']
    procedure DoSomething1;
    procedure WMSize(var Message: TWMSize); message WM_SIZE; // <- Unknown directive: 'message'
  end;

  TMyCheckBox = class(TCheckBox, IDoSomething) // <- Undeclared identifier: 'DoSomething1'
  end;

  TMyRadioButton = class(TRadioButton, IDoSomething) // <- Undeclared identifier: 'DoSomething1'
  end;

implementation

procedure IDoSomething.DoSomething1; // Identifier redeclared: 'IDoSomething'
begin
  // do the same thing for TMyCheckBox and TRadioButton
end;

procedure IDoSomething.WMSize(var Message: TWMSize);
begin
  // handle this message the same as with TMyCheckBox and TRadioButton
end;

如何使用界面集中代码?
,以及如何在界面的帮助下共享新属性?

How do I use the Interface to centralize the code? and How can I share new properties with the help of the Interface?

推荐答案


  1. 接口方法声明中不允许使用message关键字。删除 message WM_SIZE; ,代码将正常运行,直到编译器遇到第二个问题-见下文

  1. the message keyword is not allowed in an interface method declaration. Remove message WM_SIZE; and the code will run fine until the compiler hits the second problem - see below

接口方法永远不会有自己的实现。如果 IDoSomething 接口声明方法 DoSomething1 ,则编译器将不允许编写类似类似于您的代码中的过程IDoSomething.DoSomething1; -相反,实现接口的类必须提供实现主体。

Interface methods never have their own implementations. If the IDoSomething interface declares a method DoSomething1, the compiler will not allow to write an implementation like procedure IDoSomething.DoSomething1; like in your code - instead, the class which implements the interface has to provide the implementation body.

这篇关于如何在VCL类中使用接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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