具体内容可以参见《C#与.Net技术平台演练》一书中的11-7章节 版本控制。
下面是事例代码:
using System;
class A
{
public void F1()
{
Console.WriteLine("A.F1");
}
public virtual void F2()
{
Console.WriteLine("A.F2");
}
public virtual void F3()
{
Console.WriteLine("A.F3");
}
}
class B:A
{
public virtual void F1()
{
Console.WriteLine("B.F1");
}
public override void F2()
{
Console.WriteLine("B.F2");
}
public new void F3()
{
Console.WriteLine("B.F3");
}
}
class C:B
{
public override void F1()
{
Console.WriteLine("C.F1");
}
public override void F2()
{
Console.WriteLine("C.F2");
}
public void F3()
{
Console.WriteLine("C.F3");
}
}
class App
{
static void Main(string[] args)
{
A o1=new A();
Console.WriteLine("/////////////////////");
Console.WriteLine("A o1=new A()");
Console.WriteLine("after we call the methods:");
o1.F1();
o1.F2();
o1.F3();
////////////////////////////////////////////////////
B o2=new C();
Console.WriteLine("/////////////////////");
Console.WriteLine("B o2=new C()");
Console.WriteLine("after we call the methods:");
o2.F1();
o2.F2();
o2.F3();
////////////////////////////////////////////////////
A o3=o2;
Console.WriteLine("/////////////////////");
Console.WriteLine("B o3=new C()");
Console.WriteLine("after we call the methods:");
o3.F1();
o3.F2();
o3.F3();
Console.Read();
}
}
思考后可以对比答案:
/////////////////////
A o1=new A()
after we call the methods:
A.F1
A.F2
A.F3
/////////////////////
B o2=new C()
after we call the methods:
C.F1
C.F2
B.F3
/////////////////////
B o3=new C()
after we call the methods:
A.F1
C.F2
A.F3
小结:针对面向对象的多态特性,我们常常将对象声明为一个基础类型,但是初始化为派生类型,以此在调用基础类的方法时,实现方法的动态邦定。要实现这个特性,一定要记住一个原则:基础类中的方法应该使用virtual修饰符,而派生类中的同一方法使用override修饰符,只有完整的配对使用才可以实现多态的这一个特性。
下面是事例代码:
using System;
class A
{
public void F1()
{
Console.WriteLine("A.F1");
}
public virtual void F2()
{
Console.WriteLine("A.F2");
}
public virtual void F3()
{
Console.WriteLine("A.F3");
}
}
class B:A
{
public virtual void F1()
{
Console.WriteLine("B.F1");
}
public override void F2()
{
Console.WriteLine("B.F2");
}
public new void F3()
{
Console.WriteLine("B.F3");
}
}
class C:B
{
public override void F1()
{
Console.WriteLine("C.F1");
}
public override void F2()
{
Console.WriteLine("C.F2");
}
public void F3()
{
Console.WriteLine("C.F3");
}
}
class App
{
static void Main(string[] args)
{
A o1=new A();
Console.WriteLine("/////////////////////");
Console.WriteLine("A o1=new A()");
Console.WriteLine("after we call the methods:");
o1.F1();
o1.F2();
o1.F3();
////////////////////////////////////////////////////
B o2=new C();
Console.WriteLine("/////////////////////");
Console.WriteLine("B o2=new C()");
Console.WriteLine("after we call the methods:");
o2.F1();
o2.F2();
o2.F3();
////////////////////////////////////////////////////
A o3=o2;
Console.WriteLine("/////////////////////");
Console.WriteLine("B o3=new C()");
Console.WriteLine("after we call the methods:");
o3.F1();
o3.F2();
o3.F3();
Console.Read();
}
}
思考后可以对比答案:
/////////////////////
A o1=new A()
after we call the methods:
A.F1
A.F2
A.F3
/////////////////////
B o2=new C()
after we call the methods:
C.F1
C.F2
B.F3
/////////////////////
B o3=new C()
after we call the methods:
A.F1
C.F2
A.F3
小结:针对面向对象的多态特性,我们常常将对象声明为一个基础类型,但是初始化为派生类型,以此在调用基础类的方法时,实现方法的动态邦定。要实现这个特性,一定要记住一个原则:基础类中的方法应该使用virtual修饰符,而派生类中的同一方法使用override修饰符,只有完整的配对使用才可以实现多态的这一个特性。
本文通过具体的 C# 代码示例介绍了如何利用 virtual 和 override 关键字来实现多态特性,展示了不同类继承关系中方法调用的实际效果。

1083

被折叠的 条评论
为什么被折叠?



