Delphi:如何向后代添加不同的构造函数?
更新:我最初的例子有点复杂。这是一个简单的 8 行示例,在一个代码块中解释了所有内容。以下不编译给出警告:
TComputer = class(TObject)
public
constructor Create(Cup: Integer); virtual;
end;
TCellPhone = class(TComputer)
public
constructor Create(Cup: Integer; Teapot: string); virtual;
end;
注意:这个问题是我正在进行的有关Delphi中构造函数的微妙之处的系列问题中的第3部分
原始问题
How can i add a现有类的构造函数?
让我们举一个假设的例子(即我在 SO 编辑器中输入的例子,因此它可能会也可能不会编译):
TXHTMLStream = class(TXMLStream)
public
...
end;
进一步假设 TXHTMLStream
的正常使用涉及执行大量重复的操作 这
var
xs: TXHTMLStream;
begin
xs := TXHTMLStream.Create(filename);
xs.Encoding := UTF32;
xs.XmlVersion := 1.1;
xs.DocType := 'strict';
xs.PreserveWhitespace := 'true';
...
xs.Save(xhtmlDocument);
假设我想创建一个构造函数来简化所有样板设置代码:
TXHTMLStream = class(TXMLStream)
public
constructor Create(filename: string; Encoding: TEncoding); virtual;
end;
constructor TXHTMLStream.Create(filename: string; Encoding: TEncoding);
begin
inherited Create(filename);
xs.Encoding := Encoding;
xs.XmlVersion := 1.1;
xs.DocType := 'strict';
xs.PreserveWhitespace := True;
...
end;
将对象的使用简化为:
var
xs: TXHTMLStream;
begin
xs := TXHTMLStream.Create(filename, UTF32);
xs.Save(xhtmlDocument);
除了现在Delphi抱怨我的新构造函数隐藏了旧构造函数。
方法“Create”隐藏基类型“TXMLStream”的虚拟方法
我当然不是意思隐藏祖先创建 - 我想要 两者兼而有之。
如何将构造函数(具有不同签名)添加到后代类,同时保留祖先构造函数,以便仍然可以使用它?
Update: The example i originally had was kind of complex. Here's a simple 8 line example that explains everything in one code block. The following does not compile gives a warning:
TComputer = class(TObject)
public
constructor Create(Cup: Integer); virtual;
end;
TCellPhone = class(TComputer)
public
constructor Create(Cup: Integer; Teapot: string); virtual;
end;
Note: This question is part 3 in my ongoing series of questions about the subtlties of constructors in Delphi
Original question
How can i add a constructor to an existing class?
Let's give an hypothetical example (i.e. one that i'm typing up in here in the SO editor so it may or may not compile):
TXHTMLStream = class(TXMLStream)
public
...
end;
Further assume that the normal use of TXHTMLStream
involved performing a lot of repeated code before it can be used:
var
xs: TXHTMLStream;
begin
xs := TXHTMLStream.Create(filename);
xs.Encoding := UTF32;
xs.XmlVersion := 1.1;
xs.DocType := 'strict';
xs.PreserveWhitespace := 'true';
...
xs.Save(xhtmlDocument);
Assume that i want to create a constructor that simplifies all that boilerplate setup code:
TXHTMLStream = class(TXMLStream)
public
constructor Create(filename: string; Encoding: TEncoding); virtual;
end;
constructor TXHTMLStream.Create(filename: string; Encoding: TEncoding);
begin
inherited Create(filename);
xs.Encoding := Encoding;
xs.XmlVersion := 1.1;
xs.DocType := 'strict';
xs.PreserveWhitespace := True;
...
end;
That simplifies usage of the object to:
var
xs: TXHTMLStream;
begin
xs := TXHTMLStream.Create(filename, UTF32);
xs.Save(xhtmlDocument);
Except now Delphi complains that my new constructor hides the old constructor.
Method 'Create' hides virtual method of base type 'TXMLStream'
i certainly didn't mean to hide the ancestor create - i want both.
How do i add a constructor (with a different signature) to a descendant class, while keeping the ancestor constructor so it can still be used?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我的第一反应是使用
overload
关键字,如下所示:编辑:感谢 Ian 的编辑,它从我的答案中得出了答案。我想我是因为勇敢才得到它的,所以我将提供一个更完整的例子:
结果是:
My immediate reaction is to use the
overload
keyword, as in:Edit: Thanks Ian for the edit, which makes an answer out of my answer. I would like to think that I got it for bravery, so I am going to contribute a fuller example:
with the result being:
您可以创建两个新的重载构造函数,例如:
You could create two new overloaded constructors, for example:
另一种可能性是编写一个具有默认参数值的新构造函数,其中具有非默认参数的签名部分与基类中的原始构造函数相匹配:
Another possibility is to write a new constructor with default parameter values where the part of the signature with non-default parameters matches the original constructor in the base class:
还要记住,构造函数不必被称为 Create。旧版本的 Delphi 没有方法重载,因此您必须使用不同的名称:
...
两个构造函数现在都可用。
Also remember that constructors don't HAVE to be called Create. Older versions of Delphi didn't have method overloading, so you had to use different names:
...
Both constructors will now be available.