• 首页
  • 新手教程库
  • 在线工具
  • 视频教程
  • 代码片段库
  • 更多功能
    • 在线测验
    • 手机APP
Hostwinds建站首选4刀起支持Linux/Win(支付宝)
PacificRack $12/年起三网线路优化(可支付宝)
国外VPS哪个最好?
搬瓦工VPS-新增日本机房限量方案年付69
HostDare CN2 GIA线路4k无压力
美国和欧洲vps
  1. 首页
  2. 新手教程库
  3. Microsoft技术教程
  4. C# - 多态性
C# - 概述 C# - 概述 C# - 环境 C# - 程序结构 C# - 基本语法 C# - 数据类型 C# - 类型转换 C# - 变量 C# - 常量和文字 C# - 运营商 C# - 决策 C# - 循环 C# - 封装 C# - 方法 C# - Nullables C# - 数组 C# - 字符串 C# - 结构 C# - 枚举 C# - 类 C# - 继承 C# - 多态性 C# - 运算符重载 C# - 接口 C# - 命名空间 C# - 预处理器指令 C# - 正则表达式 C# - 异常处理 C# - 文件I / O. C# - 属性 C# - 反思 C# - 属性 C# - 索引器 C# - 代表 C# - 活动 C# - 收藏 C# - 泛型 C# - 匿名方法 C# - 不安全代码 C# - 多线程 C# - 有用的资源
教 程 目 录
上一节
下一节  

多态性这个词意味着有很多形式.在面向对象的编程范例中,多态通常表示为"一个接口,多个函数".

多态可以是静态的或动态的.在静态多态性中,在编译时确定对函数的响应.在动态多态性中,它是在运行时决定的.

静态多态性

链接函数的机制编译期间的对象称为早期绑定.它也称为静态绑定. C#提供了两种实现静态多态的技术.它们是 :

  • 函数重载

  • 运算符重载

我们将在下一章讨论运算符重载.

函数重载

您可以有多个定义相同范围内的相同函数名称.函数的定义必须通过参数列表中的参数的类型和/或数量彼此不同.您不能重载仅因返回类型而不同的函数声明.

以下示例显示使用函数 print()打印不同的数据类型 :

using System;

namespace PolymorphismApplication {
   class Printdata {
      void print(int i) {
         Console.WriteLine("Printing int: {0}", i );
      }
      void print(double f) {
         Console.WriteLine("Printing float: {0}" , f);
      }
      void print(string s) {
         Console.WriteLine("Printing string: {0}", s);
      }
      static void Main(string[] args) {
         Printdata p = new Printdata();
         
         // Call print to print integer
         p.print(5);
         
         // Call print to print float
         p.print(500.263);
         
         // Call print to print string
         p.print("Hello C++");
         Console.ReadKey();
      }
   }
}

编译并执行上述代码时,会产生以下结果 :

Printing int: 5
Printing float: 500.263
Printing string: Hello C++
动态多态性

C#允许您创建用于提供接口的部分类实现的抽象类.当派生类继承它时,实现完成. Abstract 类包含抽象方法,这些方法由派生类实现.派生类具有更专业的功能.

以下是关于抽象类的规则 :

  • 您无法创建抽象类的实例

  • 您不能在抽象类之外声明抽象方法

  • 当一个类声明密封时,它不能被继承,抽象类不能被声明为密封.

以下程序演示了一个抽象类 :

using System;

namespace PolymorphismApplication {
   abstract class Shape {
      public abstract int area();
   }
   
   class Rectangle:  Shape {
      private int length;
      private int width;
      
      public Rectangle( int a = 0, int b = 0) {
         length = a;
         width = b;
      }
      public override int area () { 
         Console.WriteLine("Rectangle class area :");
         return (width * length); 
      }
   }
   class RectangleTester {
      static void Main(string[] args) {
         Rectangle r = new Rectangle(10, 7);
         double a = r.area();
         Console.WriteLine("Area: {0}",a);
         Console.ReadKey();
      }
   }
}

编译并执行上述代码时,会产生以下结果 :

Rectangle class area :
Area: 70

如果要在要继承的类中实现的类中定义函数,则使用虚拟函数.虚函数可以在不同的继承类中以不同方式实现,并且将在运行时决定对这些函数的调用.

动态多态通过抽象类和

以下程序演示了这个 :

using System;

namespace PolymorphismApplication {
   class Shape {
      protected int width, height;
      
      public Shape( int a = 0, int b = 0) {
         width = a;
         height = b;
      }
      public virtual int area() {
         Console.WriteLine("Parent class area :");
         return 0;
      }
   }
   class Rectangle: Shape {
      public Rectangle( int a = 0, int b = 0): base(a, b) {

      }
      public override int area () {
         Console.WriteLine("Rectangle class area :");
         return (width * height); 
      }
   }
   class Triangle: Shape {
      public Triangle(int a = 0, int b = 0): base(a, b) {
      }
      public override int area() {
         Console.WriteLine("Triangle class area :");
         return (width * height / 2); 
      }
   }
   class Caller {
      public void CallArea(Shape sh) {
         int a;
         a = sh.area();
         Console.WriteLine("Area: {0}", a);
      }
   }  
   class Tester {
      static void Main(string[] args) {
         Caller c = new Caller();
         Rectangle r = new Rectangle(10, 7);
         Triangle t = new Triangle(10, 5);
         
         c.CallArea(r);
         c.CallArea(t);
         Console.ReadKey();
      }
   }
}

编译并执行上述代码时,会产生以下结果 :

Rectangle class area:
Area: 70
Triangle class area:
Area: 25
上一节
下一节  

相关新手教程:

Powershell教程
VBA教程
高级Excel教程
MFC 教程
C#教程
vb.net教程
Microsoft Project教程
LinQ教程
IT屋 ©2016-2020 京ICP备14011762号 鄂公网安备42018502004713号 站点地图 站点标签 意见&反馈  SiteMap <免责申明> 本站内容来源互联网,如果侵犯您的权益请联系我们删除.
  • 首页
  • 教程
  • 工具
  • 视频
  • 代码
  • 联系站长