using
System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main( string [] args)
{
// A a = new A();
// B b = new B();
C c = new C();
// b.GetYear();
}
}
public class A
{
public A()
{
Console.WriteLine( " DEBUG: A constructing " );
this .GetYear();
}
public virtual void GetYear()
{
Console.WriteLine( " A " );
}
}
public class B:A
{
public B(): base ()
{
Console.WriteLine( " DEBUG: B constructing " );
this .GetYear();
}
public override void GetYear()
{
// base.GetYear();
Console.WriteLine( " B " );
}
}
public class C : B
{
public C()
{
Console.WriteLine( " DEBUG : C constructing " );
this .GetYear();
}
public override void GetYear()
{
// base.GetYear();
Console.WriteLine( " C " );
}
}
}
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main( string [] args)
{
// A a = new A();
// B b = new B();
C c = new C();
// b.GetYear();
}
}
public class A
{
public A()
{
Console.WriteLine( " DEBUG: A constructing " );
this .GetYear();
}
public virtual void GetYear()
{
Console.WriteLine( " A " );
}
}
public class B:A
{
public B(): base ()
{
Console.WriteLine( " DEBUG: B constructing " );
this .GetYear();
}
public override void GetYear()
{
// base.GetYear();
Console.WriteLine( " B " );
}
}
public class C : B
{
public C()
{
Console.WriteLine( " DEBUG : C constructing " );
this .GetYear();
}
public override void GetYear()
{
// base.GetYear();
Console.WriteLine( " C " );
}
}
}
运行结果:一定大出你的意料吧!
如果把代码改一下:
using
System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication2
... {
class Program
...{
static void Main(string[] args)
...{
//A a = new A();
C c = new C();
// c.GetYear();
//b.GetYear();
}
}
public class A
...{
public A()
...{
Console.WriteLine("DEBUG: A constructing");
this.GetYear();
}
protected void GetYear()
...{
Console.WriteLine("A");
}
}
public class B:A
...{
public B():base()
...{
Console.WriteLine("DEBUG: B constructing");
this.GetYear();
}
protected void GetYear()
...{
//base.GetYear();
Console.WriteLine("B");
}
}
public class C : B
...{
public C():base()
...{
Console.WriteLine("DEBUG: C constructing");
//base.GetYear();
this.GetYear();
}
protected void GetYear()
...{
//base.GetYear();
Console.WriteLine("C");
}
}
}
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication2
... {
class Program
...{
static void Main(string[] args)
...{
//A a = new A();
C c = new C();
// c.GetYear();
//b.GetYear();
}
}
public class A
...{
public A()
...{
Console.WriteLine("DEBUG: A constructing");
this.GetYear();
}
protected void GetYear()
...{
Console.WriteLine("A");
}
}
public class B:A
...{
public B():base()
...{
Console.WriteLine("DEBUG: B constructing");
this.GetYear();
}
protected void GetYear()
...{
//base.GetYear();
Console.WriteLine("B");
}
}
public class C : B
...{
public C():base()
...{
Console.WriteLine("DEBUG: C constructing");
//base.GetYear();
this.GetYear();
}
protected void GetYear()
...{
//base.GetYear();
Console.WriteLine("C");
}
}
}
通过这两个例子,终于好像彻底明白了虚函数和new(子类屏蔽父类的方法的关键字).
不过还有更cool点儿的.....请看下面的例子,仔细看小心头晕哦,哈:)
using
System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication2
... {
class Program
...{
static void Main(string[] args)
...{
//A a = new A();
//C c = new C();
D d = new D();
// c.GetYear();
//b.GetYear();
}
}
public class A
...{
public A()
...{
Console.WriteLine("DEBUG: A constructing");
this.GetYear();
}
protected virtual void GetYear()
...{
Console.WriteLine("A");
}
}
public class B:A
...{
public B():base()
...{
Console.WriteLine("DEBUG: B constructing");
this.GetYear();
}
//protected override void GetYear()
//{
// //base.GetYear();
// Console.WriteLine("B");
//}
}
public class C : B
...{
public C():base()
...{
Console.WriteLine("DEBUG: C constructing");
//base.GetYear();
this.GetYear();
}
protected override void GetYear()
...{
//base.GetYear();
Console.WriteLine("C");
}
//protected new void GetYear()
//{
// //base.GetYear();
// Console.WriteLine("C");
//}
}
public class D : C
...{
public D()
...{
Console.WriteLine("DEBUG: D contructing");
this.GetYear();
}
}
}
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication2
... {
class Program
...{
static void Main(string[] args)
...{
//A a = new A();
//C c = new C();
D d = new D();
// c.GetYear();
//b.GetYear();
}
}
public class A
...{
public A()
...{
Console.WriteLine("DEBUG: A constructing");
this.GetYear();
}
protected virtual void GetYear()
...{
Console.WriteLine("A");
}
}
public class B:A
...{
public B():base()
...{
Console.WriteLine("DEBUG: B constructing");
this.GetYear();
}
//protected override void GetYear()
//{
// //base.GetYear();
// Console.WriteLine("B");
//}
}
public class C : B
...{
public C():base()
...{
Console.WriteLine("DEBUG: C constructing");
//base.GetYear();
this.GetYear();
}
protected override void GetYear()
...{
//base.GetYear();
Console.WriteLine("C");
}
//protected new void GetYear()
//{
// //base.GetYear();
// Console.WriteLine("C");
//}
}
public class D : C
...{
public D()
...{
Console.WriteLine("DEBUG: D contructing");
this.GetYear();
}
}
}