今天发现C#虚函数简直妙不可言....
来源:互联网 发布:登录界面模板带源码 编辑:程序博客网 时间:2024/05/17 06:26
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();
}
}
}
怎么样,发现了吧.虚函数和new的确有很多相似的地方,甚至如果仅以实现程序为目的的话,应该可以互换的,不是吗?
听起来怎么有点玄啊!没错,肯定不是的.是很相似,不过肯定有不一样的地方.可在哪里呢?
- 今天发现C#虚函数简直妙不可言....
- RecyclerView妙不可言
- RecyclerView妙不可言
- 今天发现一个BUG
- 今天发现了自己
- 今天无意中发现了
- 今天发现这个网站
- 今天无意中发现DJFocus
- 今天发现了新大陆
- 今天刚刚发现的
- 今天发现这里不错
- 今天才发现
- 今天发现一个SQL技巧
- 今天发现一个新病毒 ,救人一命!
- 今天发现CSDNBLOG有点问题。
- 今天发现几本好书
- 今天发现一个好网站
- 今天发现2个空值问题
- birt初学时遇到的几个问题,适合初学人员参考
- Translator in HiveMind
- 扫描仪接口程序
- 中文分词和搜索引擎
- Yu姐姐
- 今天发现C#虚函数简直妙不可言....
- SQL SERVER中一些对象的名称更改
- SQL语句大全
- 關于注釋,注釋規范
- 韩国结婚照
- SQL Server2000索引结构及使用方法
- google的秘密入口
- 童趣集锦!笑晕了!(组图)
- 为什么采用Struts框架呢?