C#教程(四):多态

1、介绍

1.1 什么是多态

在C#中,多态性(Polymorphism)是面向对象编程中的一个重要概念,它允许不同类的对象对同一消息做出响应,即同一个方法可以在不同的对象上产生不同的行为。C#中的多态性可以通过以下几种方式实现:

1.2 为什么需要多态

多态性能够提高代码的灵活性和可扩展性,使得程序可以更轻松地适应不同的需求和情境。在C#中,这种特性有助于编写更具有组织性和可维护性的代码

2、实现方式

2.1 方法重载

2.1.1 什么是方法重载

在同一个类中定义多个方法,它们具有相同的名称但具有不同的参数列表。编译器会根据方法参数的类型、顺序和数量来区分调用不同的方法。

2.2 实例

创建MyClass.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace Polymorphism
{
   
    class MyClass
    {
   
        public void DoSomething(int num)
        {
   
            Console.WriteLine($"我是DoSomething之我接受的是int类型的参数:{num}");
        }

        public void DoSomething(string text)
        {
   
            Console.WriteLine($"我是DoSomething之我接受的是string类型的参数:{text}");
        }
    }
}


运行效果
在这里插入图片描述

2.2 方法重写

2.2.1 概念

在继承关系中,子类可以重写(覆盖)父类的虚方法或抽象方法,以实现特定于子类的行为。

2.2.2 实例

创建Animal.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace Polymorphism
{
   
    class Animal
    {
   
        public virtual void MakeSound()
        {
   
            Console.WriteLine("我能发出声音。");
        }
    }

    class Dog : Animal
    {
   
        public override void MakeSound()
        {
   
            Console.WriteLine("汪汪汪...");
        }
    }

    class Cat : Animal
    {
   
        public override void MakeSound()
        {
   
            Console.WriteLine("喵喵喵...");
        }
    }
}

运行结果
在这里插入图片描述

2.3 接口

2.3.1 概念

接口定义了一组方法、属性和事件的契约,类可以实现一个或多个接口。通过接口,不同的类可以共享相同的行为特征。

2.3.2 实例

创建Shape.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace Polymorphism
{
   
    interface IShape
    {
   
        double CalculateArea();
    }

    // 创建圆
    class Circle : IShape
    {
   
        public double Radius {
    get; set; }

        public double CalculateArea()
        {
   
            return Math.PI * Radius * Radius;
        }
    }
    // 创建矩形类
    class Rectangle : IShape
    {
   
        public double Width {
    get; set; }
        public double Height {
    get; set; }

        public double CalculateArea()
        {
   
            return Width * Height;
        }
    }
}

运行结果
在这里插入图片描述

3、Program.cs

以上三个实例的类实例化代码均在下面代码

using System;

namespace Polymorphism
{
   
    class Program
    {
   
        static void Main(string[] args)
        {
   
        	// 实例1
            //MyClass myClass = new MyClass();
            //myClass.DoSomething(7);
            //myClass.DoSomething("凯文");

			// 实例2
            //Dog dog = new Dog();
            //dog.MakeSound();

            //Cat cat = new Cat();
            //cat.MakeSound();

			// 实例3
            Circle circle = new Circle();
            circle.Radius = 2;
            double area1 = circle.CalculateArea();
            Console.WriteLine($"圆形面积:{area1}");

            Rectangle rectangle = new Rectangle();
            rectangle.Width = 2;
            rectangle.Height = 2;
            double area2 = rectangle.CalculateArea();
            Console.WriteLine($"矩形面积:{area2}");
        }
    }
}

相关推荐

  1. <span style='color:red;'>C</span>++<span style='color:red;'>多</span><span style='color:red;'>态</span>

    C++

    2023-12-27 22:10:01      26 阅读
  2. 八股文 c++

    2023-12-27 22:10:01       28 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-27 22:10:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-27 22:10:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-27 22:10:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-27 22:10:01       20 阅读

热门阅读

  1. 14-网络安全框架及模型-分层防护模型

    2023-12-27 22:10:01       35 阅读
  2. python初试五

    2023-12-27 22:10:01       39 阅读
  3. sed常用简说

    2023-12-27 22:10:01       36 阅读
  4. Spring系列:基于Spring-Jdbc实现事务

    2023-12-27 22:10:01       30 阅读
  5. 简述 tcp 和 udp的区别?

    2023-12-27 22:10:01       35 阅读
  6. python哈希算法实现

    2023-12-27 22:10:01       39 阅读
  7. 【资源】stable diffusion常用checkpoint

    2023-12-27 22:10:01       38 阅读
  8. 教你轻松看懂以太网报文

    2023-12-27 22:10:01       36 阅读
  9. python——sort函数sorted函数以及lambda函数

    2023-12-27 22:10:01       28 阅读
  10. Unix常用命令整理

    2023-12-27 22:10:01       41 阅读