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

Delphi Tdictionary继承

发布时间:2020-12-15 10:03:49 所属栏目:大数据 来源:网络整理
导读:我尝试从Tdictionary继承,不知何故默认比较器丢失.这就是我在本质上所做的: type TinpVar = class end; TinputVars = class(Tdictionarystring,TinpVar) end; TLVRvars = class(TinputVars) constructor create; end; constructor TLVRvars.create; begin i
我尝试从Tdictionary继承,不知何故默认比较器丢失.这就是我在本质上所做的:
type
  TinpVar = class
  end;
  TinputVars = class(Tdictionary<string,TinpVar>)
  end;
  TLVRvars = class(TinputVars)
    constructor create;
  end;

 constructor TLVRvars.create;
 begin
   inherited;
 end;

 var LVRvars : TLVRvars;

 begin
   LVRvars:=TLVRvars.create;

有了这个结构,我会在LVRvars中添加一个键/值对时获得AV.最终我发现这可以通过将继承类的构造函数更改为

constructor TLVRvars.create;
begin
  inherited create;
end;

我不明白为什么我必须这样做.虽然我的问题解决了,我还是想知道.

解决方法

在你的构造函数
inherited;

调用与构造函数相同参数列表的构造函数.你的构造函数没有参数,所以继承调用TObject中的do nothing构造函数.不仅你失去了比较人,而且你的实例缺少了其他必要的施工步骤.

当你更换它

inherited Create;

编译器代替执行正常的方法分辨率.它查找类祖先列表,并调用它可以的第一个??方法.在这种情况下,这是:

constructor Create(ACapacity: Integer = 0); overload;

因此,您的实例已正确创建.

文档在这里:http://docwiki.embarcadero.com/RADStudio/en/Methods#Inherited

主要摘录如下:

If inherited is followed by the name of a member,it represents a normal method call

When inherited has no identifier after it,it refers to the inherited
method with the same name as the enclosing method or,if the enclosing
method is a message handler,to the inherited message handler for the
same message. In this case,inherited takes no explicit parameters,
but passes to the inherited method the same parameters with which the
enclosing method was called. For example:

06003

occurs frequently in the implementation of constructors. It calls the
inherited constructor with the same parameters that were passed to the
descendant.

这很奇怪,不是吗从表面看,令人惊讶的是,不同的方法被称为.关键是,平原继承导致参数列表的精确匹配.你的方法没有参数.

另一方面继承了Create是一种标准的方法调用.在后一种情况下,您最终使用一个参数调用一个方法,使用该参数的默认值.所以虽然看起来你正在调用一个无参数的构造函数,但你不是.您传递一个参数,ACapacity,值为0.

(编辑:李大同)

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

    推荐文章
      热点阅读