usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
namespaceConsoleApplication2
{
classProgram
{
staticvoidMain(string[]args)
{
//Aa=newA();
//Bb=newB();
Cc=newC();
//b.GetYear();
}
}
publicclassA
{
publicA()
{
Console.WriteLine("DEBUG:Aconstructing");
this.GetYear();
}
publicvirtualvoidGetYear()
{
Console.WriteLine("A");
}
}
publicclassB:A
{
publicB():base()
{
Console.WriteLine("DEBUG:Bconstructing");
this.GetYear();
}
publicoverridevoidGetYear()
{
//base.GetYear();
Console.WriteLine("B");
}
}
publicclassC:B
{
publicC()
{
Console.WriteLine("DEBUG:Cconstructing");
this.GetYear();
}
publicoverridevoidGetYear()
{
//base.GetYear();
Console.WriteLine("C");
}
}
}
usingSystem.Collections.Generic;
usingSystem.Text;
namespaceConsoleApplication2
{
classProgram
{
staticvoidMain(string[]args)
{
//Aa=newA();
//Bb=newB();
Cc=newC();
//b.GetYear();
}
}
publicclassA
{
publicA()
{
Console.WriteLine("DEBUG:Aconstructing");
this.GetYear();
}
publicvirtualvoidGetYear()
{
Console.WriteLine("A");
}
}
publicclassB:A
{
publicB():base()
{
Console.WriteLine("DEBUG:Bconstructing");
this.GetYear();
}
publicoverridevoidGetYear()
{
//base.GetYear();
Console.WriteLine("B");
}
}
publicclassC:B
{
publicC()
{
Console.WriteLine("DEBUG:Cconstructing");
this.GetYear();
}
publicoverridevoidGetYear()
{
//base.GetYear();
Console.WriteLine("C");
}
}
}
运行结果:一定大出你的意料吧!
如果把代码改一下:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
namespaceConsoleApplication2
...{
classProgram
...{
staticvoidMain(string[]args)
...{
//Aa=newA();
Cc=newC();
//c.GetYear();
//b.GetYear();
}
}
publicclassA
...{
publicA()
...{
Console.WriteLine("DEBUG:Aconstructing");
this.GetYear();
}
protectedvoidGetYear()
...{
Console.WriteLine("A");
}
}
publicclassB:A
...{
publicB():base()
...{
Console.WriteLine("DEBUG:Bconstructing");
this.GetYear();
}
protectedvoidGetYear()
...{
//base.GetYear();
Console.WriteLine("B");
}
}
publicclassC:B
...{
publicC():base()
...{
Console.WriteLine("DEBUG:Cconstructing");
//base.GetYear();
this.GetYear();
}
protectedvoidGetYear()
...{
//base.GetYear();
Console.WriteLine("C");
}
}
}
通过这两个例子,终于好像彻底明白了虚函数和new(子类屏蔽父类的方法的关键字).
不过还有更cool点儿的.....请看下面的例子,仔细看小心头晕哦,哈:)
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
namespaceConsoleApplication2
...{
classProgram
...{
staticvoidMain(string[]args)
...{
//Aa=newA();
//Cc=newC();
Dd=newD();
//c.GetYear();
//b.GetYear();
}
}
publicclassA
...{
publicA()
...{
Console.WriteLine("DEBUG:Aconstructing");
this.GetYear();
}
protectedvirtualvoidGetYear()
...{
Console.WriteLine("A");
}
}
publicclassB:A
...{
publicB():base()
...{
Console.WriteLine("DEBUG:Bconstructing");
this.GetYear();
}
//protectedoverridevoidGetYear()
//{
////base.GetYear();
//Console.WriteLine("B");
//}
}
publicclassC:B
...{
publicC():base()
...{
Console.WriteLine("DEBUG:Cconstructing");
//base.GetYear();
this.GetYear();
}
protectedoverridevoidGetYear()
...{
//base.GetYear();
Console.WriteLine("C");
}
//protectednewvoidGetYear()
//{
////base.GetYear();
//Console.WriteLine("C");
//}
}
publicclassD:C
...{
publicD()
...{
Console.WriteLine("DEBUG:Dcontructing");
this.GetYear();
}
}
}
怎么样,发现了吧.虚函数和new的确有很多相似的地方,甚至如果仅以实现程序为目的的话,应该可以互换的,不是吗?
听起来怎么有点玄啊!没错,肯定不是的.是很相似,不过肯定有不一样的地方.可在哪里呢?
本文通过几个具体的C#代码实例,详细解析了虚函数与new关键字的使用方式及其区别,揭示了它们在面向对象编程中的作用。
2960

被折叠的 条评论
为什么被折叠?



