遗产问题 [英] inheritence question
本文介绍了遗产问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
如果我从派生类构造函数中调用基类,那么
基类构造函数在调用派生类之前执行
构造函数? :
public someclass(someparam foo):base(foo)
解决方案
基类构造函数在派生类构造函数之前总是被调用。
WhiteWizard
又名Gandalf
MCSD.NET,MCAD,MCT
" ^ MisterJingo ^"写道:
大家好,
如果我从派生类构造函数调用基类,是
基类构造函数在调用派生类之前执行
构造函数? :
public someclass(someparam foo):base(foo)
是
无论如何它很容易测试:
A级{
string s;
A(字符串s){this.s = s;}
}
B级:A {
B(字符串s):base(s)
{
Console.Write(s);
}
}
-
-
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
佛罗里达州交通局
" ^ MisterJingo ^" < mi ********* @ gmail.comwrote in message
news:11 ********************* *@m79g2000cwm.googlegr oups.com ...
大家好,
如果我从a调用基类派生类构造函数,是在调用派生类之前执行的
基类构造函数
构造函数? :
public someclass(someparam foo):base(foo)
你好,^ MisterJingo ^!
MIf我从派生类构造函数调用基类,是在调用派生类之前执行的
Mbase类构造函数
Mconstructor? :
我想下面的代码示例可以回答你的问题
公共类基地
{
public Base()
{
Console.WriteLine(" Base");
}
}
公共类儿童:基地
{
公共儿童():基地()
{
Console.WriteLine(" Child");
}
}
-
问候,Vadym Stetsyak
www: http: //vadmyst.blogspot.com
Hi all,
If I call the base class from a derived classes constructor, is the
base class constructor executed before the calling derived classes
constructor? :
public someclass( someparam foo) : base( foo)
解决方案
The base class constructor is ALWAYS called before a derived class constructor.
WhiteWizard
aka Gandalf
MCSD.NET, MCAD, MCT
"^MisterJingo^" wrote:
Hi all,
If I call the base class from a derived classes constructor, is the
base class constructor executed before the calling derived classes
constructor? :
public someclass( someparam foo) : base( foo)
Hi,
Yes
Anyway It''s very easy to test it:
class A{
string s;
A(string s){this.s = s;}
}
class B:A{
B(string s):base(s)
{
Console.Write(s);
}
}
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"^MisterJingo^" <mi*********@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...Hi all,
If I call the base class from a derived classes constructor, is the
base class constructor executed before the calling derived classes
constructor? :
public someclass( someparam foo) : base( foo)
Hello, ^MisterJingo^!
MIf I call the base class from a derived classes constructor, is the
Mbase class constructor executed before the calling derived classes
Mconstructor? :
I suppose code sample below can answer your question
public class Base
{
public Base()
{
Console.WriteLine("Base");
}
}
public class Child : Base
{
public Child() : base()
{
Console.WriteLine("Child");
}
}
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
这篇关于遗产问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文