主页
  • 主页
  • 分类
  • 热文
  • 教程
  • 面试
  • 标签
C#

C# 基础

C# 主页
C# 概述
C# 环境
C# 程序结构
C# 基本语法
C# 数据类型
C# 类型转换
C# 变量
C# 常量
C# 运算符
C# 决策结构
C# 循环结构
C# 封装
C# 方法
C# 可空类型
C# 数组
C# 字符串
C# 结构体
C# 枚举
C# 类
C# 继承
C# 多态性
C# 操作符重载
C# 接口
C# 命名空间
C# 预处理
C# 正则表达式
C# 异常
C# 文件与流

C# 高级

C# 属性(Attributes)
C# 反射
C# 属性(Properties)
C# 索引器
C# 委托
C# 事件
C# 集合
C# 泛型
C# 匿名方法
C# 不安全代码
C# 线程

基础

C# 主页
C# 概述
C# 环境
C# 程序结构
C# 基本语法
C# 数据类型
C# 类型转换
C# 变量
C# 常量
C# 运算符
C# 决策结构
C# 循环结构
C# 封装
C# 方法
C# 可空类型
C# 数组
C# 字符串
C# 结构体
C# 枚举
C# 类
C# 继承
C# 多态性
C# 操作符重载
C# 接口
C# 命名空间
C# 预处理
C# 正则表达式
C# 异常
C# 文件与流

高级

C# 属性(Attributes)
C# 反射
C# 属性(Properties)
C# 索引器
C# 委托
C# 事件
C# 集合
C# 泛型
C# 匿名方法
C# 不安全代码
C# 线程

C# 多态性


上一章 下一章

多态性的词意是具有多种形式。在面向对象的编程范式中,多态性通常表达为“一个接口,多种功能”。

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

静态多态性

将函数与对象在编译时绑定的机制称为早期绑定。这也被称为静态绑定。C# 提供了两种实现静态多态性的技术。它们分别是:

  • 函数重载
  • 运算符重载

我们在下一章中讨论运算符重载。

函数重载

您可以在相同的范围内为相同的函数名称提供多个定义。函数的定义必须通过参数列表中的类型和/或数量的不同来区分彼此。您不能仅通过返回类型不同的函数声明来实现重载。

下面的例子展示了使用函数 print() 打印不同类型的数据:

示例代码

using System;

namespace PolymorphismApplication {
   class Printdata {
      void print(int i) {
         Console.WriteLine("打印整数: {0}", i );
      }
      void print(double f) {
         Console.WriteLine("打印浮点数: {0}", f);
      }
      void print(string s) {
         Console.WriteLine("打印字符串: {0}", s);
      }
      static void Main(string[] args) {
         Printdata p = new Printdata();
         
         // 调用 print 打印整数
         p.print(5);
         
         // 调用 print 打印浮点数
         p.print(500.263);
         
         // 调用 print 打印字符串
         p.print("Hello C++");
         Console.ReadKey();
      }
   }
}

当上述代码被编译和执行时,它产生以下结果:

打印整数: 5
打印浮点数: 500.263
打印字符串: Hello C++

动态多态性

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

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

  • 您不能创建抽象类的实例;
  • 您不能在非抽象类之外声明抽象方法;
  • 当一个类被声明为 sealed 时,它不能被继承,抽象类不能被声明为 sealed。

下面的程序演示了抽象类:

示例代码

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("矩形类面积 :");
         return (width * length); 
      }
   }
   class RectangleTester {
      static void Main(string[] args) {
         Rectangle r = new Rectangle(10, 7);
         double a = r.area();
         Console.WriteLine("面积: {0}",a);
         Console.ReadKey();
      }
   }
}

当上述代码被编译和执行时,它产生以下结果:

矩形类面积 :
面积: 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("基类面积 :");
         return 0;
      }
   }
   class Rectangle: Shape {
      public Rectangle( int a = 0, int b = 0): base(a, b) {

      }
      public override int area () {
         Console.WriteLine("矩形类面积 :");
         return (width * height); 
      }
   }
   class Triangle: Shape {
      public Triangle(int a = 0, int b = 0): base(a, b) {
      }
      public override int area() {
         Console.WriteLine("三角形类面积 :");
         return (width * height / 2); 
      }
   }
   class Caller {
      public void CallArea(Shape sh) {
         int a;
         a = sh.area();
         Console.WriteLine("面积: {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();
      }
   }
}

当上述代码被编译和执行时,它产生以下结果:

矩形类面积 :
面积: 70
三角形类面积 :
面积: 25
上一章 下一章
阅读号二维码

关注阅读号

联系二维码

联系我们

© 2024 Yoagoa. All rights reserved.

粤ICP备18007391号

站点地图