转载自:MSDN
类似文章:点击打开链接
在运行时,在方法参数和集合或数组等位置,派生类的对象可以作为基类的对象处理。 发生此情况时,该对象的声明类型不再与运行时类型相同。基类可以定义并实现 虚 方法,派生类可以 重写这些方法,即派生类提供自己的定义和实现。 在运行时,客户端代码调用该方法,CLR 查找对象的运行时类型,并调用虚方法的重写方法。 因此,你可以在源代码中调用基类的方法,但执行该方法的派生类版本。派生类(子类)与基类(父类)之间强制转换不会丢失信息。
namespace PolymorphismTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { ParentClass parent = new ParentClass(); SubClass sub = new SubClass(); parent = ((ParentClass)sub); //子类 父类 实例之间来回转换不会丢失信息 SubClass subNew = (SubClass)parent; } } public class SubClass : ParentClass { public new string name = "SubClass"; public int age = 20; } public class ParentClass { string name = "ParentClass"; } }
执行到SubClass subNew = (SubClass)parent
具体结果如下图:
创建一个类层次结构,其中每个特定形状类均派生自一个公共基类。
使用虚方法通过对基类方法的单个调用来调用任何派生类上的相应方法。
public class Shape { // A few example members public int X { get; private set; } public int Y { get; private set; } public int Height { get; set; } public int Width { get; set; } // Virtual method public virtual void Draw() { Console.WriteLine("Performing base class drawing tasks"); } } class Circle : Shape { public override void Draw() { // Code to draw a circle... Console.WriteLine("Drawing a circle"); base.Draw(); } } class Rectangle : Shape { public override void Draw() { // Code to draw a rectangle... Console.WriteLine("Drawing a rectangle"); base.Draw(); } } class Triangle : Shape { public override void Draw() { // Code to draw a triangle... Console.WriteLine("Drawing a triangle"); base.Draw(); } } class Program { static void Main(string[] args) { // Polymorphism at work #1: a Rectangle, Triangle and Circle // can all be used whereever a Shape is expected. No cast is // required because an implicit conversion exists from a derived // class to its base class. System.Collections.Generic.List<Shape> shapes = new System.Collections.Generic.List<Shape>(); shapes.Add(new Rectangle()); shapes.Add(new Triangle()); shapes.Add(new Circle()); // Polymorphism at work #2: the virtual method Draw is // invoked on each of the derived classes, not the base class. foreach (Shape s in shapes) { s.Draw(); } // Keep the console open in debug mode. Console.WriteLine("Press any key to exit."); Console.ReadKey(); } } /* Output: Drawing a rectangle Performing base class drawing tasks Drawing a triangle Performing base class drawing tasks Drawing a circle Performing base class drawing tasks */
在 C# 中,每个类型都是多态的,因为包括用户定义类型在内的所有类型都继承自 Object。
关注对象原则:调用子类还是父类的方法,取决于创建的对象是子类对象还是父类对象
重写基类中的虚拟成员。
继承最接近的基类方法而不重写它
定义隐藏基类实现的成员的新非虚实现
public class BaseClass { public virtual void DoWork() { } public virtual int WorkProperty { get { return 0; } } } public class DerivedClass : BaseClass { public override void DoWork() { } public override int WorkProperty { get { return 0; } } }
DerivedClass B = new DerivedClass(); B.DoWork(); // Calls the new method. BaseClass A = (BaseClass)B; A.DoWork(); // Also calls the new method.
public class BaseClass { public void DoWork() { WorkField++; } public int WorkField; public int WorkProperty { get { return 0; } } } public class DerivedClass : BaseClass { public new void DoWork() { WorkField++; } public new int WorkField; public new int WorkProperty { get { return 0; } } }
DerivedClass B = new DerivedClass(); B.DoWork(); // Calls the new method. BaseClass A = (BaseClass)B; A.DoWork(); // Calls the old method.
public class A { public virtual void DoWork() { } } public class B : A { public override void DoWork() { } }
public class C : B { public sealed override void DoWork() { } }
public class D : C { public new void DoWork() { } }
public class Base { public virtual void DoWork() {/*...*/ } } public class Derived : Base { public override void DoWork() { //Perform Derived's work here //... // Call DoWork on base class base.DoWork(); } }
有关详细信息,请参阅 base。
说明 |
---|
|