加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 这两组代码有什么区别

发布时间:2020-12-15 09:41:08 所属栏目:大数据 来源:网络整理
导读:这两段代码之间有什么区别 type IInterface1 = interface procedure Proc1; end; IInterface2 = interface procedure Proc2; end; TMyClass = class(TInterfacedObject,IInterface1,IInterface2) protected procedure Proc1; procedure Proc2; end; 以下内容
这两段代码之间有什么区别

type
  IInterface1 = interface
    procedure Proc1;
  end;

  IInterface2 = interface
    procedure Proc2;
  end;

  TMyClass = class(TInterfacedObject,IInterface1,IInterface2)
  protected
    procedure Proc1;
    procedure Proc2;
  end;

以下内容:

type
  IInterface1 = interface
    procedure Proc1;
  end;

  IInterface2 = interface(Interface1)
    procedure Proc2;
  end;

  TMyClass = class(TInterfacedObject,IInterface2)
  protected
    procedure Proc1;
    procedure Proc2;
  end;

如果它们是同一个,那么它们是否有任何优点或可读性问题.

我想第二个意味着你不能编写一个实现IInterface2的类而不实现IInterface1,而第一个你可以.

解决方法

首先,我假设第二个例子的IInterface2声明是一个错字,应该是

IInterface2 = interface(Interface1)

因为继承本身是荒谬的(即使编译器接受了它).

而“继承”是回答你问题的关键词.在示例1中,两个接口是完全独立的,您可以实现一个,另一个或两者都没有问题.在示例2中,你是正确的,你不能实现interface2而不实现interface1,但之所以如此,是因为它使interface1成为interface2的一部分.

那么,差异主要在于结构和组织,而不仅仅是可读性.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读