今天发现C#虚函数简直妙不可言....
2006-11-23 14:46
281 查看
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;
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();
}
}
}
怎么样,发现了吧.虚函数和new的确有很多相似的地方,甚至如果仅以实现程序为目的的话,应该可以互换的,不是吗?
听起来怎么有点玄啊!没错,肯定不是的.是很相似,不过肯定有不一样的地方.可在哪里呢?
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");
}
}
}
通过这两个例子,终于好像彻底明白了虚函数和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();
}
}
}
怎么样,发现了吧.虚函数和new的确有很多相似的地方,甚至如果仅以实现程序为目的的话,应该可以互换的,不是吗?
听起来怎么有点玄啊!没错,肯定不是的.是很相似,不过肯定有不一样的地方.可在哪里呢?
相关文章推荐
- 今天在使用C#的ASP.NET给Access的数据库中添加新记录发现的问题
- 今天才搞清楚:原来在C#中对象作函数参数,是引用传递:)
- 今天发现c#中的实例后的对象是不能调用静态方法,以前学Java的时候是可以的.
- 今天发现c#中的实例后的对象是不能调用静态方法,以前学Java的时候是可以的.
- 汇总c#.net常用函数和方法集
- C# 如何调用WebServer函数
- 今天的工作发现了4年前的“bug一枚”
- c# 常用函数
- C#导出Excel的函数及注意事项
- 用C#获取当前函数名,当前代码行,源代码文件
- C# 16进制转换10进制相关函数详解
- C#编码简单性之函数篇(如何编写简短的C#代码,随时更新)
- C#.NET函数和方法集
- C#调用C++动态链接库中的函数指针与函数指针结构
- 今天才发现And和AndAlso的区别
- 今天遇到的一个函数指针的问题
- C#中ArrayList 与 string、string[]数组 的转换 (转学习:今天做项目时遇到一个arrylist转换问题,做记录)
- C#常见和常用函数表
- c#.net常用的小函数集1
- 汇总c#常用函数和方法集