Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 动态多态性解释_C#_Oop_Polymorphism - Fatal编程技术网

C# 动态多态性解释

C# 动态多态性解释,c#,oop,polymorphism,C#,Oop,Polymorphism,我在寻找关于多态性的一些很容易解释的东西,我发现了一个例子。但我看不到它在代码中的实际实现位置,以及它是如何实现的?你们能帮我吗 代码: 那么它是在这里实施的, public override int Area() { Console.WriteLine("Area ="); return lenght * widht; } 关键字表示该方法是多态的(这里它与Shape实现中的关键字一起工作)。然而,更明显的证明方法是: Rectangle r = new Rectangle(

我在寻找关于多态性的一些很容易解释的东西,我发现了一个例子。但我看不到它在代码中的实际实现位置,以及它是如何实现的?你们能帮我吗

代码:


那么它是在这里实施的,

public override int Area()
{
    Console.WriteLine("Area =");
    return lenght * widht;
}
关键字表示该方法是多态的(这里它与
Shape
实现中的关键字一起工作)。然而,更明显的证明方法是:

Rectangle r = new Rectangle(10, 9);

Shape anUnknownShape = r;  //could be also a circle, ellipse, etc.

// here we "forget" what's the actual shape we're dealing with

double area = anUnknownShape.Area(); // Area is abstract - a polymorphic call

Console.WriteLine(area);

在标有第二条注释的行中,调用是多态的-具有类型为
Shape
的引用,您不知道将准确调用哪个
区域
-它可能是为
矩形
编写的实现,也可能是为另一个矩形编写的实现,从
或任何子类化
Shape
的实现。特定的实现将在运行时根据实际的类型进行选择。

这意味着函数重写也会引用多态性,换句话说,确切地说是多态性。“方法重写”与“方法重载”(静态多态性)相反。方法重写是子类中现有方法的新定义。在堆上运行时创建的所有对象。实际绑定在运行时完成。(后期绑定)一个更具解释性的网站:。@AbidAli是的,这些术语是严格相关的。注意我在
override
abstract
关键字下给出的链接。
Rectangle r = new Rectangle(10, 9);

Shape anUnknownShape = r;  //could be also a circle, ellipse, etc.

// here we "forget" what's the actual shape we're dealing with

double area = anUnknownShape.Area(); // Area is abstract - a polymorphic call

Console.WriteLine(area);