Interface mapping 映射

Interface mapping

本问题已经有最佳答案,请猛点这里访问。

在工作中,我们主要使用Delphi,但也使用一些c。所以今天一所大学问了我一个有趣的问题:我如何在C中执行intreface映射?既然我不知道这是个问题:

首先是Delphi示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
unit UnitInterfaceMapping;

interface

type
  IMyFirstInterface = interface(IInterface)
    procedure DoSomethingInteresting;
    procedure DoSomethingElse;
  end;

  IMyNextInterface = interface(IInterface)
    procedure DoSomethingInteresting;
  end;

  TMyCombinedObject = class(TInterfacedObject, IMyFirstInterface, IMyNextInterface)
  private
    procedure IMyFirstInterface.DoSomethingInteresting = DoSomethingInterestingFirst;
    procedure IMyNextInterface.DoSomethingInteresting = DoSomethingInterestingNext;
  public
    procedure DoSomethingInterestingFirst;
    procedure DoSomethingInterestingNext;
    procedure DoSomethingElse;
  end;

implementation

uses
  VCL.Dialogs;
{ TMyCombinedObject }

procedure TMyCombinedObject.DoSomethingElse;
begin
  ShowMessage('DoSomethingElse');
end;

procedure TMyCombinedObject.DoSomethingInterestingFirst;
begin
  ShowMessage('DoSomethingInterestingFirst');
end;

procedure TMyCombinedObject.DoSomethingInterestingNext;
begin
  ShowMessage('DoSomethingInterestingNext');
end;

end.

形成一个我称之为我的代码的表格:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
uses
  UnitInterfaceMapping;

procedure TForm14.Button1Click(Sender: TObject);
var
  MyFirstInterface: IMyFirstInterface;
  MyNextInterface: IMyNextInterface;
begin
  MyFirstInterface := TMyCombinedObject.Create;
  MyFirstInterface.DoSomethingInteresting;
  MyFirstInterface.DoSomethingElse;

  MyNextInterface := TMyCombinedObject.Create;
  MyNextInterface.DoSomethingInteresting;
end;

结果是三个对话框,每个方法一个。

然后我试着把它移植到C:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;

namespace InterfaceMapping
{

    public interface IMyFirstInterface
    {
        void DoSomethingInteresting();
        void DoSomethingElse();
    }


    public interface IMyNextInterface
    {
        void DoSomethingInteresting();
    }

    public class CombinedObject : IMyFirstInterface, IMyNextInterface
    {
        public void DoSomethingInteresting()
        {
            throw new NotImplementedException();
        }

        void IMyFirstInterface.DoSomethingElse()
        {
            throw new NotImplementedException();
        }

        void IMyFirstInterface.DoSomethingInteresting()
        {
            throw new NotImplementedException();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            new CombinedObject() <== Problem
        }
    }
}

主要是在创建CombinedObject的新实例时,我只看到DoSomethingInteresting方法。

简而言之:如何在C中执行接口映射#


为了从IMyFirstInterface接口访问方法,需要显式定义对象的类型:

1
2
IMyFirstInterface obj = new CombinedObject();
obj.DoSomethingInteresting(); //Method is accessible

另一方面,如果您实例化类型为CombinedObject的对象,上面的行将调用void IMyFirstInterface.DoSomethingInteresting()方法:

1
2
var obj = new CombinedObject();
obj.DoSomethingInteresting();

将调用隐式实现的接口的方法:public void DoSomethingInteresting()