关于继承的一个小程序
using System;
using System.Collections.Generic;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
B b = new B();
b.print();
C c = new C();
c.print();
Console.ReadKey();
}
}
class A
{
public int a = 3;
}
class B : A
{
public new int a = 4;
public virtual void print()
{
Console.WriteLine(base.a);
}
}
class C : B
{
public override void print()
{
Console.WriteLine(base.a);
}
}
}
会输出using System.Collections.Generic;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
B b = new B();
b.print();
C c = new C();
c.print();
Console.ReadKey();
}
}
class A
{
public int a = 3;
}
class B : A
{
public new int a = 4;
public virtual void print()
{
Console.WriteLine(base.a);
}
}
class C : B
{
public override void print()
{
Console.WriteLine(base.a);
}
}
}
3
4
而如果把class C中的print函数整个删除,会输出
3
3
张旋(zxsoft)
如对本文有什么疑问,请在下面写下留言,谢谢!