解决Delphi中由接口继承引起的命名冲突

解决Delphi中由接口继承引起的命名冲突

本文介绍了解决Delphi中由接口继承引起的命名冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这3个接口

unit Interfaces;

interface
type
  IA = interface['{86367399-1601-4FDD-89CF-362E762D948E}']
  procedure doSomething;
end;

  IB = interface['{070B9364-4A57-4E5A-BBA7-FBF1C6A48C5A}']
  procedure doSomething;
end;

  IC =interface(IB)['{DAC8021C-42CB-40EC-A001-466909044EC3}']
  procedure doSomething;
end;

implementation

end.

如何在实现IA和IC的类中解决命名问题?
我对IA和IC没有问题,但我如何实现IB?

How do i resolve naming conficts in a class that implements IA and IC?I have no problem with IA and IC but how do i implement IB?

type
   myClass = class(TInterfacedObject, IA, IC)
    procedure doASomething;
    procedure doBSomething;
    procedure doCSomething;
    procedure IA.doSomething = doASomething;
    //        ???            = doBSomething;
    procedure IC.doSomething = doCSomething;

  end;


推荐答案

根据我的:

它真的有意义吗?扩展 IB (即 IC )重新声明相同的方法?由于 IC 扩展 IB 程序doSomething; 已经可用。作为使用 IC 的实现的客户,您如何以与IB的不同的方式调用IC的 doSomething doSomething

As per my comment:
Does it really make sense for an extension of IB (namely IC) to redeclare an identical method? Since IC extends IB, procedure doSomething; is already available. As a client using an implementation of IC, how would you call IC's doSomething differently from IB's doSomething?

因此,如果您执行以下操作,则应该没有问题。

So if you do the following, you should have no trouble.

IC =interface(IB)['{DAC8021C-42CB-40EC-A001-466909044EC3}']
  //procedure doSomething; //Don't redeclare an existing method in an extension.
end;

type
   myClass = class(TInterfacedObject,
     //Explicitly state that you're implementing IB
     IA, IB, IC)

     procedure doASomething;
     procedure doBSomething;
     procedure doCSomething;
     procedure IA.doSomething = doASomething;
     //And you'll be able to define IB's method resolution.
     procedure IB.doSomething = doBSomething;
     procedure IC.doSomething = doCSomething;
   end;

您可能还会在和答案很有用,因为它讨论了为什么需要明确说明类实现哪些接口。

You may also find some of the information in this question and answers useful, as it discusses why you need to explicitly state which interfaces a class implements.

这篇关于解决Delphi中由接口继承引起的命名冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 20:10