什么是多态性,它的用途是什么,如何使用它?
当前回答
什么是多态性?
多态性是一种能力:
调用专门化类型实例上的操作时,只知道其泛化类型,而调用专门化类型的方法,而不调用泛化类型的方法: 这就是动态多态性。 定义几个具有保存名称但参数不同的方法: 这是静态多态。
首先是历史定义,也是最重要的。
多态用于什么?
它允许创建类层次结构的强类型一致性,并做一些神奇的事情,比如管理不同类型的对象列表,而不知道它们的类型,只知道它们的父类型之一,以及数据绑定。
强类型和弱类型
样本
这里有一些形状,如点、线、矩形和圆,它们的Draw()操作要么不接受任何参数,要么使用参数设置超时来删除它们。
public class Shape
{
public virtual void Draw()
{
DoNothing();
}
public virtual void Draw(int timeout)
{
DoNothing();
}
}
public class Point : Shape
{
int X, Y;
public override void Draw()
{
DrawThePoint();
}
}
public class Line : Point
{
int Xend, Yend;
public override Draw()
{
DrawTheLine();
}
}
public class Rectangle : Line
{
public override Draw()
{
DrawTheRectangle();
}
}
var shapes = new List<Shape> { new Point(0,0), new Line(0,0,10,10), new rectangle(50,50,100,100) };
foreach ( var shape in shapes )
shape.Draw();
这里Shape类和Shape. draw()方法应该被标记为抽象。
它们不是用来理解的。
解释
如果没有多态性,使用抽象-虚拟-重写,在解析形状时,只调用Spahe.Draw()方法,因为CLR不知道要调用什么方法。它调用我们所作用的类型的方法,这里的类型是Shape,因为列表声明。所以代码什么都不做。
通过多态性,CLR能够推断我们使用所谓的虚拟表所操作的对象的真实类型。它调用good方法,如果Shape是Point调用Shape。draw ()所以代码画出了形状。
更多的阅读
c# -多态性(1级)
Java中的多态性(要求等级2)
多态性(c#编程指南)
虚方法表
其他回答
从理解和应用PHP多态性,感谢Steve Guidetti。
Polymorphism is a long word for a very simple concept. Polymorphism describes a pattern in object oriented programming in which classes have different functionality while sharing a common interface. The beauty of polymorphism is that the code working with the different classes does not need to know which class it is using since they’re all used the same way. A real world analogy for polymorphism is a button. Everyone knows how to use a button: you simply apply pressure to it. What a button “does,” however, depends on what it is connected to and the context in which it is used — but the result does not affect how it is used. If your boss tells you to press a button, you already have all the information needed to perform the task. In the programming world, polymorphism is used to make applications more modular and extensible. Instead of messy conditional statements describing different courses of action, you create interchangeable objects that you select based on your needs. That is the basic goal of polymorphism.
通常这指的是A类型对象的行为与b类型对象相似的能力。在面向对象编程中,这通常是通过继承来实现的。一些维基百科的链接来阅读更多:
面向对象编程中的多态性 类型多态性
编辑:固定破碎的链接。
一般来说,它是一种使用相同或表面上相似的API来为许多不同类型的对象提供接口的能力。有多种形式:
Function overloading: defining multiple functions with the same name and different parameter types, such as sqrt(float), sqrt(double) and sqrt(complex). In most languages that allow this, the compiler will automatically select the correct one for the type of argument being passed into it, thus this is compile-time polymorphism. Virtual methods in OOP: a method of a class can have various implementations tailored to the specifics of its subclasses; each of these is said to override the implementation given in the base class. Given an object that may be of the base class or any of its subclasses, the correct implementation is selected on the fly, thus this is run-time polymorphism. Templates: a feature of some OO languages whereby a function, class, etc. can be parameterised by a type. For example, you can define a generic "list" template class, and then instantiate it as "list of integers", "list of strings", maybe even "list of lists of strings" or the like. Generally, you write the code once for a data structure of arbitrary element type, and the compiler generates versions of it for the various element types.
在编码术语中,多态性是指您的对象可以通过继承等方式以多种类型存在。如果你创建了一个名为“Shape”的类,它定义了对象的边数,那么你可以创建一个继承它的新类,如“Square”。当你随后创建一个“Square”实例时,你可以根据需要将它从“Shape”前后转换为“Square”。
多态:
根据类的实例而不是引用变量的类型执行不同的执行。
接口类型引用变量可以引用实现该接口的任何类实例。