C# 中的多态性

WBOY
发布: 2024-09-03 15:26:15
原创
977 人浏览过

多态性是在 C# 等面向对象编程语言中实现的概念,其中运算符或函数方法在整个执行过程中可以采用多种形式。它广泛用于程序中继承的实现,分为两种方法,即运算符重载和函数重载。简而言之,多态性可以解释为一种使用派生类更改基类的技术,其中两个类都拥有不同的属性。

下图说明了多态性的工作原理:

C# 中的多态性

该图说明粘土是制作锅、碗和玩具物体的母体。所有这些物体都彼此不同,但它们都具有粘土的特性。这三个物体即使是由相同的材料粘土制成,也具有不同的形状和不同的用途。

多态性的形式

以下是多态性的两种形式:

1.运算符重载

运算符可以根据其操作的操作数类型给出不同的输出。这称为运算符重载。例如,运算符“+”可以对两个整数执行加法,同时它可以连接两个字符串。因此,同一个运算符可以以两种不同的方式使用。加法运算符通常将两个数字相加。然而,在复数的情况下,加法有点不同,相应的实部和虚部是分别相加的。下面是一个示例代码,展示了如何重载‘+’运算符来添加复数。

代码:

using System;
namespace Overload {
class Complex
{
public double x;
public double y;
// no-argument constructor
public Complex() {}
// parameterized constructor
public Complex(double real, double img)
{
x = real;
y = img;
}
// Overloading of Binary "+" operator
public static Complex operator + (Complex c1,
Complex c2)
{
Complex c3 = new Complex();
c3.x = c1.x + c2.x;
c3.y = c1.y + c2.y;
return c3;
}
// function to display result
public void display()
{
Console.WriteLine("{0} + {1}i", x,y);
}
}
class CalNum {
// Driver Code
static void Main(string[] args)
{
Complex num1 = new Complex(2.5,3.5);
Complex num2 = new Complex(1.2,6.5);
Complex num3 = num1 + num2;
Console.Write("c1 = ");
num1.display();
Console.Write("c2 = ");
num2.display();
Console.Write("c3 = ");
num3.display();
}
}
}
登录后复制

输出:

C# 中的多态性

此输出说明了运算符重载以添加两个复数。代码显示了运算符重载以及函数重载。

2.函数重载

函数可以根据参数数量及其返回类型和函数返回类型给出不同的输出。上述代码还涵盖了使用构造函数的函数重载。代码中有2个构造函数;一个是默认构造函数,保留为空,而另一个是参数化构造函数,用于使用作为参数提到的值来初始化复数。

以图中所示为例,功能是塑造粘土,笔画方向是提供用于塑造粘土的构件。根据不同的笔划方向,粘土分别变成了锅、碗和玩具。

代码:

using System;
using System.Collections.Generic;
public class Clay
{
// A few example members
public int Height { get; set; }
public int strokeDirection { get; set; }
// Virtual method
public virtual void Mould()
{
Console.WriteLine("Beginning to mould the clay");
}
}
class Pot : Clay
{
public override void Mould()
{
// Code to make a pot Console.WriteLine("Making a pot");
base.Mould();
}
}
class Bowl : Clay
{
public override void Mould()
{
// Code to make a bowl
Console.WriteLine("Making a bowl");
base.Mould();
}
}
class Toy : Clay
{
public override void Mould()
{
// Code to make a toy
Console.WriteLine("Making a toy");
base.Mould();
}
}
class Program
{
static void Main(string[] args)
{
// Illustrating Polymorphism #1: a Pot, Bowl and Toy
// can all be used wherever Clay is expected. No cast is
// needed because an implicit conversion exists from a derived
// class to its base class.
var items = new List<Clay>
{
new Pot(),
new Bowl(),
new Toy()
};
// Polymorphism at work #2: the virtual method Mould is
// invoked on each of the derived classes, not the base class.
foreach (var item in items)
{
item.Mould();
}
}
}
登录后复制

输出:

C# 中的多态性

正如您在代码中可能已经注意到的,clay 类是父类,其子类是pot、toy 和bowl 类。方法“Mould”在父类和子类中定义,具有相同的函数签名。因此,当创建子类的对象并调用方法 Mould 时,基类方法将被子类方法覆盖。因此,我们看到了子类方法的输出。上面的代码显示了方法重写而不是重载以说明多态性。

C# 中多态性的注意点

C#中多态性需要牢记的要点:

  • 重载与覆盖不同。重载具有不同的函数签名,而重写具有相同的函数签名。
  • 多态是指改变子类中超类的行为。
  • 继承是指在子类中使用超类的结构和行为。

以下是一些关键要点:

  • 多态性是面向对象编程的主要范例之一。
  • 函数重载根据继承类中函数的返回类型和参数修改函数。
  • 运算符重载根据给定参数修改运算符定义。
  • 多态用于实现继承。它决定在运行时使用方法重写来调用子类方法。

以上是C# 中的多态性的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!