Polymorphism (C# Programming Guide)
https://msdn.microsoft.com/en-us/library/ms173152.aspx
Polymorphism is often referred to as the third pillar of object-oriented programming, after encapsulation and inheritance. 面向对象编程的三个特性:封装,继承,多态
Polymorphism is a Greek word that means "many-shaped" and it has two distinct aspects:
At run time, objects of a derived class may be treated as objects of a base class in places such as method parameters and collections or arrays. When this occurs, the object's declared type is no longer identical to its run-time type.
Base classes may define and implement virtual methods, and derived classes can override them, which means they provide their own definition and implementation. At run-time, when client code calls the method, the CLR looks up the run-time type of the object, and invokes that override of the virtual method. Thus in your source code you can call a method on a base class, and cause a derived class's version of the method to be executed.
Virtual methods enable you to work with groups of related objects in a uniform统一的 way.
For example, suppose you have a drawing application that enables a user to create various kinds of shapes on a drawing surface外观.
You do not know at compile time which specific types of shapes the user will create.
However, the application has to keep track of all the various types of shapes that are created, and it has to update them in response to user mouse actions.
You can use polymorphism to solve this problem in two basic steps:
Create a class hierarchy层级 in which each specific shape class derives from a common base class.
Use a virtual method to invoke the appropriate method on any derived class through a single call to the base class method.
First, create a base class called Shape, and derived classes such as Rectangle, Circle, and Triangle.
Give the Shape class a virtual method called Draw, and override it in each derived class to draw the particular shape that the class represents.
Create a List<Shape> object and add a Circle, Triangle and Rectangle to it.
To update the drawing surface, use a foreach loop to iterate迭代 through the list and call the Draw method on each Shapeobject in the list.
Even though each object in the list has a declared type of Shape, it is the run-time type (the overridden version of the method in each derived class) that will be invoked.
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
*/
In C#, every type is polymorphic because all types, including user-defined types, inherit from Object.
Polymorphism Overview
Virtual Members
When a derived class inherits from a base class, it gains获得 all the methods, fields, properties and events of the base class.
The designer of the derived class can choose whether to
override virtual members in the base class,
inherit the closest base class method without overriding it
define new non-virtual implementation of those members that hide the base class implementations
A derived class can override a base class member only if the base class member is declared as virtual or abstract.
The derived member must use the override keyword to explicitly indicate that the method is intended to participate in virtual invocation.
The following code provides an example:
public class BaseClass
{
public virtual void DoWork() { }
public virtual int WorkProperty
{
get { return ; }
}
}
public class DerivedClass : BaseClass
{
public override void DoWork() { }
public override int WorkProperty
{
get { return ; }
}
}
Fields cannot be virtual; only methods, properties, events and indexers can be virtual.
When a derived class overrides a virtual member, that member is called even when an instance of that class is being accessed as an instance of the base class.
The following code provides an example:
DerivedClass B = new DerivedClass(); //B是子类的实例
B.DoWork(); // Calls the new method. BaseClass A = (BaseClass)B; //即便是把B转换为父类A的实例,调用DoWork,还是调用的子类重写的方法
A.DoWork(); // Also calls the new method.
Virtual methods and properties enable derived classes to extend a base class without needing to use the base class implementation of a method.
For more information, see Versioning with the Override and New Keywords (C# Programming Guide).
An interface provides another way to define a method or set of methods whose implementation is left to derived classes.
For more information, see Interfaces (C# Programming Guide).
Hiding Base Class Members with New Members
If you want your derived member to have the same name as a member in a base class,
but you do not want it to participate in virtual invocation, you can use the new keyword.
The new keyword is put before the return type of a class member that is being replaced.
The following code provides an example:
public class BaseClass
{
public void DoWork() { WorkField++; }
public int WorkField;
public int WorkProperty
{
get { return ; }
}
} public class DerivedClass : BaseClass
{
public new void DoWork() { WorkField++; }//用new替换掉override
public new int WorkField;
public new int WorkProperty
{
get { return ; }
}
}
Hidden base class members can still be accessed from client code by casting the instance of the derived class to an instance of the base class.
For example:
DerivedClass B = new DerivedClass();
B.DoWork(); // Calls the new method. 调用子类的方法 BaseClass A = (BaseClass)B;
A.DoWork(); // Calls the old method. 调用父类的方法
Preventing Derived Classes from Overriding Virtual Members
Virtual members remain virtual indefinitely, regardless of how many classes have been declared between the virtual member and the class that originally declared it.
If class A declares a virtual member, and class B derives from A, and class C derives from B,
class C inherits the virtual member,and has the option to override it, regardless of whether class B declared an override for that member.
The following code provides an example:
public class A
{
public virtual void DoWork() { }
}
public class B : A
{
public override void DoWork() { }
}
A derived class can stop virtual inheritance by declaring an override as sealed.
This requires putting the sealed keyword before the override keyword in the class member declaration.
The following code provides an example:
public class C : B
{
public sealed override void DoWork() { }
}
In the previous example, the method DoWork is no longer virtual to any class derived from C.
It is still virtual for instances of C, even if they are cast to type B or type A.
Sealed methods can be replaced by derived classes by using the new keyword, as the following example shows:
public class D : C
{
public new void DoWork() { }
}
In this case, if DoWork is called on D using a variable of type D, the new DoWork is called.
If a variable of type C, B, or A is used to access an instance of D, a call to DoWork will follow the rules of virtual inheritance, routing those calls to the implementation of DoWork on class C.
D d = new D();
d.DoWork();//调用的是类D里面的new方法
A a = d;
a.DoWork();//调用的是类C里面的方法 //调用的是从A往后递推,有sealed修饰的那个方法
Accessing Base Class Virtual Members from Derived Classes
A derived class that has replaced or overridden a method or property can still access the method or property on the base class using the base keyword.
The following code provides an example:
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();
}
}
For more information, see base.
Note:
It is recommended that virtual members use base to call the base class implementation of that member in their own implementation.
Letting the base class behavior occur enables the derived class to concentrate on implementing behavior specific to the derived class.
If the base class implementation is not called, it is up to the derived class to make their behavior compatible with the behavior of the base class.
In This Section
Versioning with the Override and New Keywords (C# Programming Guide)
Knowing When to Use Override and New Keywords (C# Programming Guide)
See Also
Polymorphism (C# Programming Guide)的更多相关文章
- Interfaces (C# Programming Guide)
https://msdn.microsoft.com/en-us/library/ms173156.aspx An interface contains definitions for a group ...
- 【IOS笔记】View Programming Guide for iOS -1
原文:View Programming Guide for iOS View and Window Architecture Views and windows present your applic ...
- Quartz 2D Programming Guide
Quartz 2D Programming Guide 官方文档: Quartz 2D Programming Guide 译文: Quartz 2D编程指南(1) - 概览 Quartz 2D编程 ...
- [IoLanguage]Io Programming Guide[转]
Io Programming Guide Introduction Perspective Getting Started Downloading Installing Binaries Ru ...
- Structured Streaming Programming Guide结构化流编程指南
目录 Overview Quick Example Programming Model Basic Concepts Handling Event-time and Late Data Fault T ...
- GraphX学习笔记——Programming Guide
学习的资料是官网的Programming Guide https://spark.apache.org/docs/latest/graphx-programming-guide.html 首先是Gra ...
- View Programming Guide for iOS_读书笔记[正在更新……]
原文:View Programming Guide for iOS 1 Introduction 先熟悉一下基本概念. Window Windows do not have any visible c ...
- OpenGL® ES 3.0 Programming Guide - Book Website
OpenGL® ES 3.0 Programming Guide - Book Website http://opengles-book.com sample codes in GitHub: htt ...
- 对Spark2.2.0文档的学习3-Spark Programming Guide
Spark Programming Guide Link:http://spark.apache.org/docs/2.2.0/rdd-programming-guide.html 每个Spark A ...
随机推荐
- scala学习(1)----map和flatMap的区别
转载:https://www.cnblogs.com/wbh1000/p/9846401.html 两者的区别主要在于action后得到的值 例子: import org.apache.spark.{ ...
- 解决移动端 footer fixd 定位被键盘顶起来的方案
直接上代码: $(document).ready(function () { var u = navigator.userAgent; var isAndroid = u.indexOf('Andro ...
- 01java基础
01.java基础-18/07/09 1.System.out.print();和System.out.println();的区别是什么 System.out.println();打印的时候自带了换行 ...
- 洛谷——P1775 古代人的难题_NOI导刊2010提高(02)&& P1936 水晶灯火灵(斐波那契数列)
P1775 古代人的难题_NOI导刊2010提高(02) P1936 水晶灯火灵 斐波那契数列 1.x,y∈[1…k],且x,y,k∈Z 2.(x^2-xy-y^2)^2=1 给你一个整数k,求一组满 ...
- 「 CODE[VS] P2853 」 方格游戏
题目大意 给定一张 $n\times n$ 的网格.每个格子上都有一个系数 $a$,先下 $A$ 和 $B$ 两人选择两条 $(1,1)\rightarrow (n,n)$ 路径.要求着两条路径不能相 ...
- java.lang unsupported classversion解决方法
设置编译的jdk和运行的jdk环境版本是否一致.一般都是jdk导致的.刚开始用jdk1.6编译运行,死活不行,换成jdk1.7运行也是1.7,ok
- centos7进入救援模式,修复错误配置
因某些修改操作,导致系统重启后无法正常启动,此时可进入救援模式,修复错误配置即可. OS:centos 7 1.重启系统后,进入grup引导页面,选中第一项然后按“e” 进入编辑模式: 2.通过↓键找 ...
- Java垃圾回收是如何工作的?
本教程是为了理解基本的Java垃圾回收以及它是如何工作的.这是垃圾回收教程系列的第二部分.希望你已经读过了第一部分:<Java 垃圾回收介绍>. Java 垃圾回收是一项自动化的过程,用来 ...
- 【Codeforces 675D】Tree Construction
[链接] 我是链接,点我呀:) [题意] 依次序将数字插入到排序二叉树当中 问你每个数字它的父亲节点上的数字是啥 [题解] 按次序处理每一个数字 对于数字x 找到最小的大于x的数字所在的位置i 显然, ...
- JRebel 7.1.5 插件下载 安装 激活 结合 IntelliJ IDEA--自动编译进行热部署---
Intellij IDEA 安装和配置jrebel进行项目的热部署 https://www.cnblogs.com/a8457013/p/7866625.html Intellij IDEA 使用jr ...