new 覆盖 和 override的 区别
***敏感词已被过滤***
一定要找回搞技术的状态,不然就早点回家去搞点事,比如卖豆浆油条啥的。
废话不多说,上代码看效果, 体会一下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Testvirtual
{
publicclass A
{
publicvirtualvoid msg()
{
Console.WriteLine("A");
}
}
publicclass B : A
{
publicnewvoid msg()
{
Console.WriteLine("B");
}
}
publicclass D : A
{
publicoverridevoid msg()
{
Console.WriteLine("d");
}
}
class Program
{
staticvoid Main(string[] args)
{
A b =new A();
A bb =new B();
B bbb =new B();
A d =new A();
A dd =new D();
D ddd =new D();
b.msg();
bb.msg();
bbb.msg();
d.msg();
dd.msg();
ddd.msg();
Console.Read();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Testvirtual
{
publicclass A
{
publicvirtualvoid msg()
{
Console.WriteLine("A");
}
}
publicclass B : A
{
publicnewvoid msg()
{
Console.WriteLine("B");
}
}
publicclass D : A
{
publicoverridevoid msg()
{
Console.WriteLine("d");
}
}
class Program
{
staticvoid Main(string[] args)
{
A b =new A();
A bb =new B();
B bbb =new B();
A d =new A();
A dd =new D();
D ddd =new D();
b.msg();
bb.msg();
bbb.msg();
d.msg();
dd.msg();
ddd.msg();
Console.Read();
}
}
}
输出:
A
A
B
A
d
d