关于继承构造函数的新手问题 [英] Novice question about inherited constructors
问题描述
我正在从Delphi转到C#并且遇到了问题:
我有以下类和表单Load事件处理程序:
公共类class1
{
公共字符串S;
公共class1(字符串aS)
{
S = aS;
}
}
公共类class2:class1
{
}
private void Form1_Load(object sender,System.EventArgs e)
{
class2 C2 = new class2(" string");
label1.Text = C2.S;
}
当我尝试编译时,我得到以下错误:
方法''class1'没有重载需要''0''参数
方法''class2'没有重载需要''1''参数
这是什么意思?
为什么不使用继承的构造函数(如德尔福)?
Oleg Subachev
这只是.NET设计的一部分,构造函数不是继承的。
如果你在基类中有一个非默认的构造函数,你还必须定义
相同的构造函数,然后将值传递给它的基础。
public class2(string aS):base(aS){...}
" Oleg Subachev" <醇** @ urvb.ru>在消息中写道
news:e
************** @ TK2MSFTNGP02.phx.gbl ...我从Delphi转到C#并且遇到了问题:
我有以下类和表单Load事件处理程序:
公共类class1
{
公共字符串S;
公共类1(字符串aS)
{
S = aS;
}
}
<公共类class2:class1
{
}
私有void Form1_Load(对象发送者,System.EventArgs e)
{
class2 C2 = new class2(" string");
label1.Text = C2.S;
}
当我尝试编译时,我收到以下错误:
<没有重载方法''class1''需要''0''参数
没有重载方法''class2''需要''1''参数
它是什么是什么意思?
为什么不使用继承的构造函数(如在Delphi中)?
Oleg Subachev
Oleg Subachev写道:我从Delphi转到C#并且遇到了问题:
< snip> ;
当我尝试编译时,我得到以下错误:
方法''class1''没有重载需要''0''参数
这是因为编译器正在为
class2创建一个默认构造函数,它试图在class1中调用无参数构造函数 - 但是
那里没有''' t one。
方法''class2''没有重载需要''1''参数
这是因为class2中没有构造函数取一个字符串,
因为构造函数不被继承。
为什么不使用继承的构造函数(如在Delphi中)?
如果继承了构造函数,那么* all * classes必须有一个
无参数构造函数,就像System.Object那样。我不知道德尔福如何绕过它,但我个人很高兴施工人员不会继承 - b $ b继承 - 能够做到这一点很好要求任何实例的某个类*必须*通过我指定的构造函数之一,
,即使已将额外的构造函数添加到基础中我认为实例创建与实例使用有些不同 - 事物
可以在创建它们之后以相同的方式处理
非常不同的创作要求。
(话虽如此,我*会喜欢某种方式来指定
构造函数*必须*存在于实现特定
接口的任何类型中,并且可能同样存在于静态方法中。为了调用那些构造函数/静态方法,需要额外的语法
,
然而 - 它确实有点复杂。)
Jon
I am moving from Delphi to C# and hve encountered the problem:
I have the following classes and form Load event handler:
public class class1
{
public string S;
public class1( string aS )
{
S = aS;
}
}
public class class2 : class1
{
}
private void Form1_Load(object sender, System.EventArgs e)
{
class2 C2 = new class2( "string" );
label1.Text = C2.S;
}
When I try to compile I get the following errors:
No overload for method ''class1'' takes ''0'' arguments
No overload for method ''class2'' takes ''1'' arguments
What does it mean ?
Why inherited constructors are not used (as in Delphi) ?
Oleg Subachev解决方案That is just part of the design of .NET that constructors are not inherited.
If you have a non-default constructor in a base class, you must also define
that same constructor and then pass the value up to its base.
public class2(string aS) : base(aS){...}
"Oleg Subachev" <ol**@urvb.ru> wrote in message
news:e
**************@TK2MSFTNGP02.phx.gbl...I am moving from Delphi to C# and hve encountered the problem:
I have the following classes and form Load event handler:
public class class1
{
public string S;
public class1( string aS )
{
S = aS;
}
}
public class class2 : class1
{
}
private void Form1_Load(object sender, System.EventArgs e)
{
class2 C2 = new class2( "string" );
label1.Text = C2.S;
}
When I try to compile I get the following errors:
No overload for method ''class1'' takes ''0'' arguments
No overload for method ''class2'' takes ''1'' arguments
What does it mean ?
Why inherited constructors are not used (as in Delphi) ?
Oleg Subachev
Oleg Subachev wrote:I am moving from Delphi to C# and hve encountered the problem:
<snip>
When I try to compile I get the following errors:
No overload for method ''class1'' takes ''0'' arguments
This is because the compiler is creating a default constructor for
class2, which tries to call a parameterless constructor in class1 - but
there isn''t one.
No overload for method ''class2'' takes ''1'' arguments
This is because there isn''t a constructor in class2 taking a string,
because constructors aren''t inherited.
Why inherited constructors are not used (as in Delphi) ?
If constructors were inherited, then *all* classes would have to have a
parameterless constructor, as System.Object does. I don''t know how
Delphi gets around it, but personally I''m glad constructors aren''t
inherited - it''s good to be able to mandate that any instance of a
certain class *must* go through one of the constructors I''ve specified,
even if extra constructors have been added to the base class.
I view instance creation as somewhat different to instance use - things
which can be treated the same way after they''ve been created often have
very different creation requirements.
(Having said that, I *would* like some way of specifying what
constructors *must* be present in any type implementing a specific
interface, and possibly likewise for static methods. Extra syntax would
be necessary in order to call those constructors/static methods,
however - it does all get a bit complicated.)
Jon
这篇关于关于继承构造函数的新手问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!