C# 多态性
来源:互联网 发布:cisco防火墙端口地址 编辑:程序博客网 时间:2024/06/05 20:46
转载自: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。
- C#多态性
- C#多态性
- c# 多态性
- c# 多态性
- c# 多态性
- C#多态性
- c# 多态性
- C# 多态性
- C#多态性
- C#多态性
- C# 多态性
- C# 多态性
- C# 多态性
- 多态性C#
- C# 多态性
- C# 多态性
- C# 多态性
- C# 多态性
- Mybatis学习(八)mybatis环境下以接口的方式进行的关联表查询
- Win32 API 选择文件和文件夹
- 安装测试Lasagne
- N简单CMS学习记录
- 设计模式
- C# 多态性
- VS2013MFC单文档工程学习笔记二 - 新建一个按钮
- Maven的使用
- CC2541 BLE源码阅读知识积累之外设从机Peripheral工作模式
- maven发布jar、aar、war等到中央库(Central Repository)的步骤
- C++的默认拷贝构造函数/深拷贝/浅拷贝详解
- Tomcat 7最大并发连接数的正确修改方法
- Linux系统编程——线程私有数据
- IO多路复用总结:select pool epoll