delphi 接口编程_Delphi编程101中的接口

本文介绍了Delphi编程中的接口,将其比作无实现的抽象类,并阐述了接口在实现可维护、可重用和灵活代码中的作用。接口定义了抽象,而实现则在具体的类中完成。此外,文章提到了接口与COM编程的关系,以及如何在不涉及COM的情况下使用接口。
摘要由CSDN通过智能技术生成

delphi 接口编程

In Delphi, "interface" has two distinct meanings. In OOP jargon, you can think of an interface as a class with no implementation. In Delphi unit definition interface section is used to declare any public sections of code that appear in a unit. This article will explain interfaces from an OOP perspective.

Delphi中 ,“接口”具有两个不同的含义。 在OOP术语中,您可以将接口视为没有实现的类。 在Delphi中,单元定义接口部分用于声明出现在单元中的代码的所有公共部分。 本文将从OOP角度解释接口。

If you are up to creating a rock-solid application in a way that your code is maintainable, reusable, and flexible the OOP nature of Delphi will help you drive the first 70% of your route. Defining interfaces and implementing them will help with the remaining 30%.

如果您打算以一种可维护,可重用和灵活的代码方式创建坚如磐石的应用程序,则Delphi的OOP性质将帮助您实现前70%的路线。 定义接口并实现它们将帮助剩下的30%。

抽象类 ( Abstract Classes )

You can think of an interface as an abstract class with all the implementation stripped out and everything that is not public removed. An abstract class in Delphi is a class that cannot be instantiated—you cannot create an object from a class marked as abstract.

您可以将接口视为一个抽象类,其中删除了所有实现,并删除了所有不公开的内容。 Delphi中的抽象类是无法实例化的类-您不能从标记为抽象的类中创建对象。

Let's take a look at an example interface declaration:

让我们看一下接口声明示例:

type 类型
IConfigChanged =
IConfigChanged = interface['{0D57624C-CDDE-458B-A36C-436AE465B477}'] 接口 ['{0D57624C-CDDE-458B-A36C-436AE465B477}'] procedure ApplyConfigChange; 程序 ApplyConfigChange; end; 结束 ;

The IConfigChanged is an interface. An interface is defined much like a class, the keyword "interface" is used instead of "class". The Guid value that follows the interface keyword is used by the compiler to uniquely identify the interface. To generate a new GUID value, just press Ctrl+Shift+G in the Delphi IDE. Each interface you define needs a unique Guid value.

IConfigChanged是一个接口。 接口的定义与类非常相似,使用关键字“接口”代替“类”。 interface关键字后面的Guid值由编译器用来唯一标识接口。 要生成新的GUID值,只需在Delphi IDE中按Ctrl + Shift + G。 您定义的每个接口都需要一个唯一的Guid值。

An interface in OOP defines an abstraction—a template for an actual class that will implement the interface—that will implement the methods defined by the interface. An interface does not actually do anything, it only has a signature for interaction with other (implementing) classes or interfaces.

OOP中的接口定义了抽象(将实现该接口的实际类的模板),该抽象将实现该接口定义的方法。 接口实际上不做任何事情,它仅具有用于与其他(实现)类或接口进行交互的签名。

The implementation of the methods (functions, procedures, and property Get/Set methods) is done in the class that implements the interface. In the interface definition, there are no scope sections (private, public, published, etc.) everything is public. An interface type can define functions, procedures (that will eventually become methods of the class that implements the interface) and properties. When an interface defines a property it must define the get/set methods - interfaces cannot define variables.

方法(函数,过程和属性Get / Set方法)的实现在实现接口的类中完成。 在接口定义中,没有作用域部分(私有,公共,已发布等),所有都是公共的。 接口类型可以定义函数,过程(最终将成为实现接口的类的方法)和属性。 接口定义属性时,必须定义获取/设置方法-接口不能定义变量。

As with classes, an interface can inherit from other interfaces.

与类一样,一个接口可以从其他接口继承。

type 类型
IConfigChangedMore =
IConfigChangedMore = interface(IConfigChanged) 接口 (IConfigChanged) procedure ApplyMoreChanges; 程序 ApplyMoreChanges; end; 结束 ;

程式设计 ( Programming )

Most Delphi developers when they think of interfaces they think of COM programming. However, interfaces are just an OOP feature of the language—they are not tied to COM specifically. Interfaces can be defined and implemented in a Delphi application without touching COM at all.

大多数Delphi开发人员在想到接口时都会想到COM编程。 但是,接口只是该语言的OOP功能-它们并不专门与COM绑定。 可以完全在不接触COM的情况下在Delphi应用程序中定义和实现接口。

实作 ( Implementation )

To implement an interface you need to add the name of the interface to the class statement, as in:

要实现接口,您需要将接口的名称添加到class语句中,如下所示:

type 类型
TMainForm =
TMainForm = class(TForm, IConfigChanged) (TForm,IConfigChanged) public 上市 procedure ApplyConfigChange; 程序 ApplyConfigChange; end; 结束 ;

In the above code a Delphi form named "MainForm" implements the IConfigChanged interface.

在上面的代码中,名为“ MainForm”的Delphi表单实现了IConfigChanged接口。

Warning: when a class implements an interface it must implement all its methods and properties. If you fail/forget to implement a method (for example: ApplyConfigChange) a compile time error 警告 :当类实现接口时,它必须实现其所有方法和属性。 如果您失败/忘记实现一种方法(例如:ApplyConfigChange),则会发生编译时错误 "E2003 Undeclared identifier: 'ApplyConfigChange'" will occur. “ E2003未声明的标识符:'ApplyConfigChange'”Warning: if you try to specify the interface without the GUID value you will receive: 警告 :如果尝试指定不带GUID值的接口,则会收到: "E2086 Type 'IConfigChanged' is not yet completely defined". “ E2086类型'IConfigChanged'尚未完全定义”

( Example )

Consider an MDI application where several forms can be displayed to the user at one time. When the user changes the application configuration, most forms need to update their display—show/hide some buttons, update label captions, etc. You would need a simple way to notify all open forms that a change in the application configuration has happened. The ideal tool for the job was an interface.

考虑一个MDI应用程序,其中可以一次向用户显示多种表单。 当用户更改应用程序配置时,大多数表单都需要更新其显示-显示/隐藏一些按钮,更新标签标题等。您将需要一种简单的方法来通知所有打开的表单应用程序配置已发生更改。 理想的工作工具是界面。

Every form that needs to be updated when the configuration changes will implement IConfigChanged. Since the configuration screen in displayed modally, when it closes the next code ensures all IConfigChanged implementing forms are notified and ApplyConfigChange is called:

配置更改时需要更新的每个表单都将实现IConfigChanged。 由于配置屏幕以模态显示,因此在关闭下一个代码时,请确保已通知所有IConfigChanged实现形式并调用ApplyConfigChange:

procedure DoConfigChange() ; 过程 DoConfigChange(); var 变种
cnt : integer;
cnt:整数;
icc : IConfigChanged;
icc:IConfigChanged; begin 开始 for cnt := 0 对于 CNT:= to -1 + Screen.FormCount 0〜-1 + Screen.FormCount do 待办事项 begin 开始 if Supports(Screen.Forms[cnt], IConfigChanged, icc) 如果支持(Screen.Forms [cnt],IConfigChanged,icc), then
icc.ApplyConfigChange;
icc.ApplyConfigChange; end; 结束 ; end; 结束 ;

The Supports function (defined in Sysutils.pas) indicates whether a given object or interface supports a specified interface. The code iterates through the Screen.Forms collection (of the TScreen object)—all the forms currently displayed in the application. If a form Screen.Forms[cnt] supports the interface, Supports returns the interface for the last parameter parameter and returns true.

Supports 函数 (在Sysutils.pas中定义)指示给定对象或接口是否支持指定的接口。 代码遍历(TScreen对象的)Screen.Forms集合(当前在应用程序中显示的所有表单)。 如果窗体Screen.Forms [cnt]支持该接口,则Supports返回最后一个参数形参的接口并返回true。

Therefore, if the form implements the IConfigChanged, the icc variable can be used to call the methods of the interface as implemented by the form. Note, of course, that every form can have its own different implementation of the ApplyConfigChange procedure.

因此,如果表单实现了IConfigChanged,则icc变量可用于调用由表单实现的接口方法。 当然,请注意,每种形式都可以具有自己的ApplyConfigChange过程的不同实现

祖先 ( Ancestors )

Any class you define in Delphi needs to have an ancestor. TObject is the ultimate ancestor of all objects and components. The above idea applies to interfaces also, the IInterface is the base class for all interfaces. IInterface defines 3 methods: QueryInterface, _AddRef and _Release.

您在Delphi中定义的任何类都必须有一个祖先。 TObject是所有对象和组件的最终祖先。 以上想法也适用于接口,IInterface是所有接口的基类。 IInterface定义了3种方法:QueryInterface,_AddRef和_Release。

This means that our IConfigChanged also has those 3 methods, but we have not implemented those. This is because TForm inherits from TComponent that already implements the IInterface for you! When you want to implement an interface in a class that inherits from TObject, make sure your class inherits from TInterfacedObject instead. Since TInterfacedObject is a TObject implementing IInterface. For example:

这意味着我们的IConfigChanged也具有这3种方法,但尚未实现。 这是因为TForm继承自已经为您实现IInterface的TComponent! 当您想在继承自TObject的类中实现接口时,请确保您的类继承自TInterfacedObject。 由于TInterfacedObject是实现IInterface的TObject。 例如:

class( ( TInterfacedObject, IConfigChanged) TInterfacedObject ,IConfigChanged) procedure ApplyConfigChange; 程序 ApplyConfigChange; end; 结束 ;

In conclusion, IUnknown = IInterface. IUnknown is for COM.

总之,IUnknown = IInterface。 IUnknown用于COM。

翻译自: https://www.thoughtco.com/interfaces-in-delphi-programming-101-1058278

delphi 接口编程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值