什么是多态性,它的用途是什么,如何使用它?


当前回答

如果有人对这些人说CUT

外科医生 发型师 这个演员

会发生什么?

外科医生开始做一个切口。 发型师会开始给某人理发。 行动者会突然停止在当前场景之外的表演, 等待导演指导。

因此,上面的表示说明了什么是OOP中的多态性(相同的名称,不同的行为)。

如果你要去参加面试,面试官要求你在我们坐在的同一个房间里讲述/展示一个多态性的生动例子,比如说-

答案-门/窗

想知道吗?

通过门/窗-人可以进来,空气可以进来,光可以进来,雨可以进来,等等。

为了更好地理解它,以一种简单的方式,我使用了上面的例子。 如果你需要代码参考,请参考上面的答案。

其他回答

多态性允许相同的例程(函数、方法)作用于不同的类型。

由于许多现有的答案将子类型与多态性混为一谈,这里有三种实现多态性的方法(包括子类型)。

参数化(泛型)多态性允许一个例程接受一个或多个类型参数,以及正常参数,并在这些类型上运行。 子类型多态性允许例程对其参数的任何子类型进行操作。 临时多态性通常使用例程重载来授予多态行为,但也可以参考其他多态性实现。

参见:

http://wiki.c2.com/?CategoryPolymorphism

https://en.wikipedia.org/wiki/Polymorphism_ (computer_science)

多态是指您可以将对象视为某个对象的通用版本,但当您访问它时,代码将确定它的确切类型并调用相关代码。

下面是一个c#的例子。在控制台应用程序中创建四个类:

public abstract class Vehicle
{
    public abstract int Wheels;
}

public class Bicycle : Vehicle
{
    public override int Wheels()
    {
        return 2;
    }
}

public class Car : Vehicle
{
    public override int Wheels()
    {
        return 4;
    }
}

public class Truck : Vehicle
{
    public override int Wheels()
    {
        return 18;
    }
}

现在在控制台应用程序模块的Main()中创建以下内容:

public void Main()
{
    List<Vehicle> vehicles = new List<Vehicle>();

    vehicles.Add(new Bicycle());
    vehicles.Add(new Car());
    vehicles.Add(new Truck());

    foreach (Vehicle v in vehicles)
    {
        Console.WriteLine(
            string.Format("A {0} has {1} wheels.",
                v.GetType().Name, v.Wheels));
    }
}

在这个例子中,我们创建了一个基类Vehicle的列表,它不知道它的每个子类有多少个轮子,但是知道每个子类负责知道它有多少个轮子。

然后我们将自行车、汽车和卡车添加到列表中。

接下来,我们可以循环遍历列表中的每个Vehicle,并将它们都视为相同的,但是当我们访问每个Vehicles 'Wheels'属性时,Vehicle类将该代码的执行委托给相关的子类。

该代码被称为多态代码,因为执行的确切代码是由在运行时引用的子类决定的。

我希望这对你有所帮助。

什么是多态性?

多态性是一种能力:

调用专门化类型实例上的操作时,只知道其泛化类型,而调用专门化类型的方法,而不调用泛化类型的方法: 这就是动态多态性。 定义几个具有保存名称但参数不同的方法: 这是静态多态。

首先是历史定义,也是最重要的。

多态用于什么?

它允许创建类层次结构的强类型一致性,并做一些神奇的事情,比如管理不同类型的对象列表,而不知道它们的类型,只知道它们的父类型之一,以及数据绑定。

强类型和弱类型

样本

这里有一些形状,如点、线、矩形和圆,它们的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#编程指南)

虚方法表

一般来说,它是一种使用相同或表面上相似的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.

在面向对象语言中,多态性允许通过同一个接口处理和处理不同的数据类型。例如,考虑c++中的继承: 类B派生自类A。类型为A*的指针(指向类A的指针)可以用来处理类A的对象和类B的对象。