C# Polymorphism
Reprinted from: MSDN
Similar articles: Click to open the link
Polymorphism (polymorphism) is A Greek word meaning "many forms," polymorphism has two distinct aspects:
When this happens , the object's declared type is no longer the same as the runtime type.
At run time, the client code calls this method, the CLR looks up the runtime type of the object, and calls the override of the virtual method method. Thus, you can call a base class method in source code, but execute the derived class version of the method.
Forcing between a derived class (subclass) and a base class (parent class) will not lose information.
-
##
Execute tonamespace 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"; } }
Copy after loginThe specific results are as follows:SubClass subNew = (SubClass)parent
Copy after login
# For example, suppose you There is a drawing application that allows users to create various shapes on the drawing surface. You don't know at compile time what specific types of shapes the user will create. But the application must keep track of all types of shapes that are created, and must update these shapes in response to user mouse operations. You can solve this problem using polymorphism in two basic steps:
- Create a class hierarchy in which each specific Shape classes are derived from a common base class.
- #Use virtual methods to call the corresponding method on any derived class with a single call to the base class method.
Provides a virtual method named Draw for the Shape class, and in Override this method in each derived class to draw the specific shape represented by that class. Create a List
C#
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。
关注对象原则:调用子类还是父类的方法,取决于创建的对象是子类对象还是父类对象
多态性概述
虚成员
派生类的设计器可以选择是否
重写基类中的虚拟成员。
继承最接近的基类方法而不重写它
定义隐藏基类实现的成员的新非虚实现
派生成员必须使用 override 关键字显式指示该方法将参与虚调用。 以下代码提供了一个示例:
C#
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; } } }
当派生类重写某个虚拟成员时,即使该派生类的实例被当作基类的实例访问,也会调用该成员。 以下代码提供了一个示例:
C#
DerivedClass B = new DerivedClass(); B.DoWork(); // Calls the new method.BaseClass A = (BaseClass)B; A.DoWork(); // Also calls the new method.
有关详细信息,请参阅使用 Override 和 New 关键字进行版本控制(C# 编程指南)。 接口提供另一种方式来定义将实现留给派生类的方法或方法集。 有关详细信息,请参阅接口(C# 编程指南)。
使用新成员隐藏基类成员
new 关键字放置在要替换的类成员的返回类型之前。 以下代码提供了一个示例:
C#
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; } } }
例如:
C#
DerivedClass B = new DerivedClass(); B.DoWork(); // Calls the new method.BaseClass A = (BaseClass)B; A.DoWork(); // Calls the old method.
阻止派生类重写虚拟成员
如果类 A 声明了一个虚拟成员,类 B 从 A 派生,类 C 从类 B 派生,则类 C 继承该虚拟成员,并且可以选择重写它,而不管类 B 是否为该成员声明了重写。 以下代码提供了一个示例:
C#
public class A { public virtual void DoWork() { } }public class B : A { public override void DoWork() { } }
这需要在类成员声明中的 override 关键字前面放置 sealed 关键字。 以下代码提供了一个示例:
C#
public class C : B { public sealed override void DoWork() { } }
即使它们转换为类型 B 或类型 A,它对于 C 的实例仍然是虚拟的。 通过使用 new 关键字,密封的方法可以由派生类替换,如下面的示例所示:
C#
public class D : C { public new void DoWork() { } }
如果使用类型为 C、B 或 A 的变量访问 D 的实例,对 DoWork 的调用将遵循虚拟继承的规则,即把这些调用传送到类 C 的 DoWork 实现。
从派生类访问基类虚拟成员
以下代码提供了一个示例:
C#
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# 多态性的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.
